我正在使用 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
. 任何建议或链接将不胜感激。