我有一個角度4.3.6應用程序與延遲加載模塊。下面是部分根路由器:這兩個例子模塊中路由器死循環第二canActivate在惰性加載模塊上的保護
const routes: Routes = [
{ path: '', redirectTo: 'fleet', pathMatch: 'full' },
{
path: '',
component: AppComponent,
canActivate: [AuthenticationGuard],
children: [
{
path: 'fleet',
loadChildren: "./modules/fleet.module",
canActivate: [AuthenticationGuard]
},
{
path: 'password/set',
loadChildren: "./modules/chooseNewPassword.module",
canActivate: [ChoosePasswordGuard]
}
]
}
]
// Exports RouterModule.forRoot(routes, { enableTracing: true });
我的孩子路由器:
艦隊:
RouterModule.forChild([
{
path: '',
component: FleetComponent,
canActivate: [AuthenticationGuard]
}
]);
選擇新密碼:
RouterModule.forChild([
{
path: '',
component: ChooseNewPasswordComponent,
canActivate: [ChoosePasswordGuard]
}
]);
的AuthenticationGuard
電話一個看起來像這樣的方法:
return this.getUserSession().map((userSession: UserSession) => {
if (userSession && userSession.ok) {
return true;
}
else if (userSession && userSession.expired) {
this.router.navigate(['password/set'])
.catch((e: Error) => console.error(e));
return true;
}
else {
window.location.replace('/');
return false;
}
}
因此,如果用戶的會話正常,它將激活路由。如果用戶的密碼已過期,則會將用戶重定向到選擇新密碼模塊。如果沒有會話,則重定向到登錄。
的ChoosePasswordGuard
做了類似的事情,但不僅保護了選擇新的密碼組件(不同的設備用於一般設置密碼):
return this.getUserSession().map((userSession: UserSession) => {
if (userSession) {
return userSession.expired;
}
else {
return false;
}
});
該模塊拆分工作過。
現在,我被困在重定向循環中。隨着路由器追蹤,我觀察以下順序。在用戶登錄並AuthenticationGuard
校正重定向到/密碼/設置模塊,並且被越區切換到ChooseNewPasswordGuard
:
- NavigationStart(ID:4,URL: '/密碼/設定')
- RoutesRecognized {ID:4,URL: 「/密碼/設置」,urlAfterRedirects: 「/密碼/設置」,狀態:RouterStateSnapshot}
- GuardsCheckStart {ID:4,URL: 「/密碼/設置」, urlAfterRedirects:UrlTree,狀態:RouterStateSnapshot}
- GuardsCheckEnd {ID:4,URL: 「/密碼/設置」,urlAfterRedirects:UrlTree,狀態:RouterStateSnapshot,shouldActivate:真}
- NavigationCancel {ID:4,URL:「/密碼/組「,reason:」「}
然後重複此循環。
(也重複,如果我更換整個ChooseNewPasswordGuard與return Observable.of(true);
)
編輯:我重定向到根頁面(/
),即使我提供的地址欄/#/password/set
......
問題:
我做了什麼錯在我的路由器(S)或警衛現在模塊是懶的裝逼這個循環?我特別困惑
shouldActivate: true
其次NavigationCancel reason: ""
。是否有事可做的事實,我在AuthenticationGuard直接重定向,而現在,這個保護被加到我的主要空的根路徑(
{ path: '', redirectTo: 'fleet', pathMatch: 'full' }
)它總是叫和重定向,甚至一度我已經設定了路徑?我是否需要在我的孩子路線和我的根路線中重複使用
canActivate
警衛?像往常一樣,歡迎任何其他意見。