我试图使用CanActivateChild限制对我的路由系统的访问,但是当我试图阻止用户导航到一组子状态时,RouteGuard不起作用,只能在CanActivate上起作用。这是我的路由模块:
export const routes = [
{ path: "", redirectTo: "/signin", pathMatch: "full" },
{ path: "signin", component: SigninComponent },
{
path: "user",
component: UserComponente,
canActivate: [AuthGuard],
canActivateChild: [AuthGuard],
children: [
{ path: "profile", component: ProfileComponent, },
{ path: "address", component: AddressComponent, },
]
},
{ path: "test", component: TestComponent, canActivate: [AuthGuard] },
];在本例中,如果我尝试访问路由test,我可以看到AuthGuard正在执行,函数canActivate被调用。
但是,当我尝试导航到任何子路由(配置文件或地址)时,什么也没有发生。canActivate和canActivateChild都不会在AuthGuard上被调用。
这是我的防护文件:
import { Injectable } from '@angular/core';
import { CanActivate, CanActivateChild } from '@angular/router';
import { getString } from 'application-settings';
import { AppConfig } from './../config';
@Injectable()
export class AuthGuard implements CanActivate, CanActivateChild {
constructor() { }
canActivate() {
console.log('canActivate executing.');
if (!getString(AppConfig.authToken)) {
alert('You need to be logged in.');
return false;
}
return true;
}
canActivateChild() {
console.log('canActivateChild executing.');
return this.canActivate();
}
}https://stackoverflow.com/questions/44423662
复制相似问题