我正在構建angular2應用程序,目前我有一個主菜單和主菜單的菜單欄和路由器插座。我添加了登錄機制,因此如果用戶未通過身份驗證,則登錄頁面將顯示在整個屏幕中,登錄後用戶將被路由到具有上述結構的主/家庭組件。登錄後路由問題
當我運行應用程序的登錄頁面加載,併成功通過身份驗證,它將我路由到主頁,但在加載菜單的主頁(如Dashboard1,Dashboard2,Report1等),鏈接工作不正常。當我點擊任何菜單欄鏈接時,我收到下面提到的錯誤。
lib/@angular/platform-browser/bundles/platform-browser.umd.js:1900 Error: Uncaught (in promise): Error: Cannot match any routes: 'Report1'
at resolvePromise (zone.js:538)
at zone.js:515
at ZoneDelegate.invoke (zone.js:323)
at Object.onInvoke (lib/@angular/core/bundles/core.umd.js:9100)
at ZoneDelegate.invoke (zone.js:322)
at Zone.run (zone.js:216)
at zone.js:571
at ZoneDelegate.invokeTask (zone.js:356)
at Object.onInvokeTask (lib/@angular/core/bundles/core.umd.js:9091)
at ZoneDelegate.invokeTask (zone.js:355)
我在2個地方的路由器出口標籤在AppLoader將以及在其中實際菜單在加載LayoutMenu部件,即1和其包含的路由器出口加載那裏的主要內容。
什麼是正確的地方有路由器插座?我在哪裏做錯了?
下面是用於routeconfig代碼
export const routes: RouterConfig = [
{ path: '', component: Login },
{ path: 'login', component: Login },
{ path: 'Home', component: Home },
{ path: '**', component: Login }
];
export const LOGIN_ROUTER_PROVIDERS = [
provideRouter(routes)
];
主頁/菜單routeconfig是如下
export const routes: RouterConfig = [
{
path: 'home',
component: Home,
// canActivate: [AuthenticationGuard],
children: [
{ path: 'Dashboard1', component: Dashboard1 },
{ path: 'Dashboard2', component: Dashboard2 },
{ path: 'Report1', component: Report1 },
{ path: 'Report2', component: Report1 },
{
path: 'ManageRedisCache',
component: ManageRedisCache,
children: [
{ path: '', component: ExtractorQueue },
{ path: 'Extractor', component: Extractor },
{ path: 'Schedule', component: Schedule },
{ path: 'CacheVisualization', component: CacheVisualization }
]
}
]
}
];
export const HOME_ROUTER_PROVIDERS = [
provideRouter(routes)
];
上述2 routeconfig經由自舉的main.ts被注入。在main.ts引導程序中,我還注入了包含該文件的AppLoader。
AppLoader組件如下所示,其中包含/加載登錄頁面。
@Component({
selector: 'my-app',
template: `<router-outlet></router-outlet>`,
directives: [Login, ROUTER_DIRECTIVES ]
})
export class AppLoader {
}
LayoutMenu組分是如下:
import { Component } from '@angular/core';
import { ROUTER_DIRECTIVES } from '@angular/router';
@Component({
selector: 'MenuLayout',
template: `
<a [routerLink]="['/Dashboard1']"><h4>Dashboards 1</h4></a>
<a [routerLink]="['/Dashboard2']"><h4>Dashboards 2</h4></a>
<a [routerLink]="['/Report1']"><h4>Reports 1</h4></a>
<div class="outer-outlet">
<router-outlet></router-outlet>
</div> `,
directives: [LeftMenu, ROUTER_DIRECTIVES]
})
export class LayoutMenu { }
我已根據您的建議更新了routeconfig,但面臨一些新錯誤。你能幫我確定問題在哪裏嗎?我還沒有實現AuthenticationGuard,首先要實現基本的路由功能工作,然後在AuthenticationGuard上工作。 – Krishnan
@Krishnan'ExtractorQueue'似乎有一個錯位的'pathMatch'屬性。添加了一個示例項目鏈接到答案。 – leovrf
優秀答案,我成功的應用到了我的項目中 –