我正在研究一個示例Angular 2應用程序,它也具有路由功能,下面是我的路由相關代碼。如何在根組件中標識頁面未找到路由[Angular 2]?
....
...
RouterModule.forRoot([
{ path: '', component: LoginComponent },
{ path: "spa/home", canActivate: [canActivateGuard], component: HomeComponent },
{ path: 'spa/about', canActivate: [canActivateGuard], component: AboutComponent },
{ path: 'spa/login', component: LoginComponent },
{ path: '**', component: PageNotFoundComponent}
])
....
....
在我的根組件中,根據當前路由,需要將「blnDisplayMenu」的值設置爲true或false。 如果當前路由是「/ spa/login」,根組件中的下面的代碼將「blnDisplayMenu」設置爲false。
ngOnInit() {
this.router.events.subscribe(e => {
if (e instanceof NavigationEnd) {
if (e.url == '/' || e.url == '/spa/login') {
this.blnDisplayMenu = false;
}
else {
this.blnDisplayMenu = true;
}
}
});
}
我的問題是如何設置「blnDisplayMenu」爲false即使當前路線是「**」。
但如何設置爲「真」的其他路線? – refactor
得到它,在其他 – refactor
一個解決方案是使用'NavigationEnd'爲其他路線和'ActivatedRoute'爲'**'路線,但我不知道它的執行順序是否會影響輸出或不[可能會覆蓋'blnDisplayMenu'值],第二種解決方案是在所有其他路徑上將'displayMenu'設置爲'true',並在'ActivatedRoute''部分將'blnDisplayMenu'設置爲'true'。 – ranakrunal9