Angular 5.1应用程序中的一个组件未触发canActivate guard
在我的Angular 5.1.1应用程序中,单个组件上的canActivate防护不会触发。当我路由到“用户”时,AuthenticationGuard从未触发,但它适用于所有其他路径 我认为这与路径为空时使用“用户”重定向到的路径有关。因此,我将redirectTo属性更改为client。第一次未触发AuthGuard,但下一次总是。对于“用户”,它仍然没有被触发 路线Angular 5.1应用程序中的一个组件未触发canActivate guard,angular,typescript,angular-ui-router,canactivate,Angular,Typescript,Angular Ui Router,Canactivate,在我的Angular 5.1.1应用程序中,单个组件上的canActivate防护不会触发。当我路由到“用户”时,AuthenticationGuard从未触发,但它适用于所有其他路径 我认为这与路径为空时使用“用户”重定向到的路径有关。因此,我将redirectTo属性更改为client。第一次未触发AuthGuard,但下一次总是。对于“用户”,它仍然没有被触发 路线 const routes: Routes = [ { path: 'error', component: Error
const routes: Routes = [
{ path: 'error', component: ErrorComponent /* , canActivate: [AuthenticationGuard] */ },
{ path: 'id_token', component: OAuthCallbackComponent, canActivate: [OAuthCallbackHandler] }, // reply URL AAD
{ path: 'client', component: ClientsComponent, canActivate: [AuthenticationGuard] },
{ path: 'colleagues', component: ColleaguesComponent, canActivate: [AuthenticationGuard]},
{ path: 'user', component: UserComponent, canActivate: [AuthenticationGuard] },
{ path: 'useroverview', component: UserOverviewComponent, canActivate: [AuthenticationGuard] },
{ path: 'login', component: LoginComponent },
{ path: '', redirectTo: 'user', pathMatch: 'full', canActivate: [AuthenticationGuard] },
{ path: 'loading', component: OAuthCallbackComponent },
{ path: 'noaccess', component: NoAccessComponent },
{ path: '**', component: NoAccessComponent }
];
AuthGuard
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
const navigationExtras: NavigationExtras = {
queryParams: { 'redirectUrl': route.url }
};
if (!this.adalService.userInfo || !this.adalService.accessToken) {
sessionStorage.clear();
sessionStorage.setItem('redirect', this.setRedirectUrl(route.url));
this.router.navigate(['login'], navigationExtras);
return false;
}
if (((route.routeConfig.path === "client" || route.routeConfig.path === "useroverview") && UserRights[sessionStorage.getItem('Role')] < 3) ||
((sessionStorage.getItem('UserType') === '66' || sessionStorage.getItem('UserType') === '74') && route.routeConfig.path === "colleagues") && UserRights[sessionStorage.getItem('Role')] === 0)
{
this.router.navigate(['404']);
return false;
}
if (sessionStorage.length === 0) {
this.spinner.show();
return this.userManService.getUserInfo().switchMap(resp => {
this.sessionUser(resp);
return this.userManService.getMyAccountData().map(data => {
this.setUserInfo(data);
$(document).trigger('Authenticated');
this.spinner.hide();
this.myMenuOption();
return true;
}).catch(() => {
this.spinner.hide();
return Observable.of(false);
});
}).catch(() => {
this.spinner.hide();
return Observable.of(false);
});
}
else
return Observable.of(true);
}
canActivate(路由:ActivatedRouteSnapshot,状态:RouterStateSnashot){
常量navigationExtras:navigationExtras={
queryParams:{'redirectUrl':route.url}
};
如果(!this.adalService.userInfo | |!this.adalService.accessToken){
sessionStorage.clear();
sessionStorage.setItem('redirect',this.setRedirectUrl(route.url));
this.router.navigate(['login'],navigationExtras);
返回false;
}
if(((route.routeConfig.path==“client”| | route.routeConfig.path==“useroverview”)&&UserRights[sessionStorage.getItem('Role')]<3)||
((sessionStorage.getItem('UserType')=='66'| | sessionStorage.getItem('UserType')=='74')&&route.routeConfig.path==“同事”)&&UserRights[sessionStorage.getItem('Role')]==0)
{
这个.router.navigate(['404']);
返回false;
}
if(sessionStorage.length==0){
this.spinner.show();
返回此.userManService.getUserInfo().switchMap(resp=>{
这是sessionUser(resp);
返回此.userManService.getMyAccountData().map(数据=>{
这个.setUserInfo(数据);
$(document.trigger('Authenticated');
this.spinner.hide();
this.myMenuOption();
返回true;
}).catch(()=>{
this.spinner.hide();
可观察到的返回(错误);
});
}).catch(()=>{
this.spinner.hide();
可观察到的返回(错误);
});
}
其他的
可观察的返回(真);
}
怎么可能没有为该组件触发AuthGuard呢?我错过了一件非常明显的事情(太愚蠢了!)。在用户路径上,我还分配了子项。因此,当我在那里设置canActivate守卫时,它工作得很好
const routes: Routes = [{
path: 'user',
component: UserComponent,
canActivate: [AuthenticationGuard],
children: [
{ path: 'invite/:key', component: ModalContainerComponent, canActivate: [AuthenticationGuard] }
]
}]
如果从路由中删除{path:'',重定向到:'user'..}是否有效?@John我可以试试,但我如何处理空路径?