2

我正在使用 Angular4 和 Vmware 的 Clarity UI for Angular 构建一个应用程序。我有一个用于导航应用程序的主标题。应用程序中有一些视图不需要侧边栏,而其他视图则需要。当用户导航到某个视图时,我想显示一个特定于特定视图的侧边栏。我还在我的应用程序中使用模块级路由。

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { MainComponent } from './main.component';

const routes: Routes = [
    {
        path: '', component: MainComponent,
        children: [
            {
                path: 'dashboard',
                loadChildren: './dashboard/dashboard.module#DashboardModule',
                data: {
                    title: 'Dashboard',
                    showSideNav: false
                }
            },
            {
                path: 'sales',
                loadChildren: './sales/sales.module#SalesModule',
                data: {
                    title: 'Sales',
                    showSideNav: true
                }
            }
        ]
    }
];

@NgModule({
    imports: [RouterModule.forChild(routes)],
    exports: [RouterModule]
})
export class MainRoutingModule { }

这是主要组件

import { Component, OnInit } from '@angular/core';
import { Router, NavigationEnd, ActivatedRoute } from '@angular/router';

@Component({
    selector: 'app-main',
    templateUrl: './main.component.html',
    styleUrls: ['./main.component.scss']
})
export class MainComponent implements OnInit {

    showSideNav: Boolean = false;

    constructor(public router: Router, public activatedRoute: ActivatedRoute) { }

    setSidenavState() {
        this.router.events
            .filter(event => event instanceof NavigationEnd)
            .map(() => this.activatedRoute)
            .map(route => {
                while (route.firstChild) {
                    route = route.firstChild;
                }

                return route;
            })
            .filter(route => route.outlet === 'primary')
            .mergeMap(route => route.data)
            .subscribe((event) => { 
                this.showSideNav = event['showSideNav'];
                console.log(this.showSideNav);
            });
    }
    ngOnInit() {
        this.setSidenavState();
        if (this.router.url === '/') {
            this.router.navigate(['/dashboard']);
        }
    }

}

这是主要模板

<clr-main-container>
    <header class="header header-6">
        <div class="branding">
            <a routerLink="/dashboard" class="brand nav-link">
                <img src="assets/img/logo-white.png">
            </a>
        </div>
        <div class="header-nav">
            <a routerLink="/dashboard" routerLinkActive="active" class="nav-link nav-text">Dashboard</a>
            <a routerLink="/sales" routerLinkActive="active" class="nav-link nav-text">Sales</a>
            <a href="" class="nav-link nav-text">Purchases</a>
            <a href="" class="nav-link nav-text">Contacts</a>
            <a href="" class="nav-link nav-text">Reports</a>
        </div>
    </header>
    <div class="content-container">
        <div class="content-area">
            <router-outlet></router-outlet>
        </div>
        <nav class="sidenav" *ngIf="showSideNav">
            <app-sidenav></app-sidenav>
        </nav>
    </div>
</clr-main-container>

在主要组件中,我有这样的设置

+-- dashboard
+-- sales
|   +-- sidenav
+-- sidenav
|   +-- sidenav.component.html
|   +-- sidenav.component.ts
+-- main-routing.module.ts
+-- main.component.html
+-- main.component.ts
+-- main.module.ts

我的问题是,app-sidenav如果showSideNav属性设置为true. 任何建议或链接将不胜感激。

4

1 回答 1

4

我相信解决您的用例的最简洁的方法就是将一些布局移动到实际的路由组件。现在,你的主模板中有这个:

<div class="content-container">
    <div class="content-area">
        <router-outlet></router-outlet>
    </div>
    <nav class="sidenav" *ngIf="showSideNav">
        <app-sidenav></app-sidenav>
    </nav>
</div>

假设您的销售模板如下所示:

<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>

通过将 sidenav 向下移动路由组件中,您的主模板变为:

<div class="content-container">
    <router-outlet></router-outlet>
</div>

您的销售模板变为:

<div class="content-area">
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
</div>
<nav class="sidenav">
    <sales-sidenav></sales-sidenav>
</nav>

如果一个组件没有 sidenav,只需不要将 sidenav 放在其模板中。不需要*ngIf. 结果是您不再使用 sidenav 信息污染您的路线,您只需将其留给处理路线本身的组件,成为应该拥有特定 sidenav 逻辑的组件。

于 2017-08-17T13:45:17.687 回答