canActivate 守卫仅在刷新后才有效
canActivate guard works only after refresh
我在所有路线(在父路线)上都有 canActivate 守卫。当我第一次去 link 时它工作正常,但是当我改变路线时它不起作用。
Guard 是关于登录用户的(如果 api returns 我登录了我 return true,否则我将它重定向到登录页面)
我该做什么?
谢谢
在您定义的同一个守卫中,实现 CanActivateChild 接口,并调用相同的逻辑。在您的路线中,同时定义 CanActivate 和 CanActivateChild。
在你身边
@Injectable()
export class MyGuard implements CanActivate, CanActivateChild {
constructor() {}
canActivate() {
// Your logic here to identify the value to return
}
canActivateChild() {
return this.canActivate();
}
}
在你的路由中
let routes:Routes = [
{
path: 'myPath',
canActivate: [ MyGuard ]
canActivateChild: [ MyGuard ],
children: [
{ path: 'mychild1', .... },
{ path: 'mychild2', .... }
]
]
阅读有关 angular.io 保护子路由的指南:https://angular.io/guide/router#canactivatechild-guarding-child-routes
我在所有路线(在父路线)上都有 canActivate 守卫。当我第一次去 link 时它工作正常,但是当我改变路线时它不起作用。 Guard 是关于登录用户的(如果 api returns 我登录了我 return true,否则我将它重定向到登录页面) 我该做什么? 谢谢
在您定义的同一个守卫中,实现 CanActivateChild 接口,并调用相同的逻辑。在您的路线中,同时定义 CanActivate 和 CanActivateChild。
在你身边
@Injectable()
export class MyGuard implements CanActivate, CanActivateChild {
constructor() {}
canActivate() {
// Your logic here to identify the value to return
}
canActivateChild() {
return this.canActivate();
}
}
在你的路由中
let routes:Routes = [
{
path: 'myPath',
canActivate: [ MyGuard ]
canActivateChild: [ MyGuard ],
children: [
{ path: 'mychild1', .... },
{ path: 'mychild2', .... }
]
]
阅读有关 angular.io 保护子路由的指南:https://angular.io/guide/router#canactivatechild-guarding-child-routes