我有一个角度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
警卫?像往常一样,欢迎任何其他意见。