我正在使用多个canActivateChild警卫,如下所示:
{path: 'admin', canActivateChild : [AdminAuthGuard, EmployeeAuthGuard], children: adminRoutes }以下是管理警卫的定义:
canActivateChild() : boolean {
const role = localStorage.getItem('role');
if(role === 'admin') {
return true;
}
else {
return false;
}
}我有非常类似的员工警卫,我正在检查作为雇员的角色。上述代码的问题是:
注意:我的守卫是同步的,但我仍然面临着这个问题。请告诉我怎么解决这个问题?
实际代码要比这复杂得多。这只是一个获得帮助的示例代码。
发布于 2018-02-26 10:27:52
在检查两个返回值的其他两个警卫之外创建一个组合保护:
class CombinedGuard {
constructor(private adminGuard: AdminAuthGuard, private employeeGuard: EmployeeAuthGuard) {}
canActivate(r, s) {
// defined your order logic here, this is a simple AND
return this.adminGuard.canActivate(r, s) && this.employeeGuard.canActivate(r,s)
}
}然后在路由中使用该CombinedGuard:
{path: 'admin', canActivateChild : [CombinedGuard], children: adminRoutes }https://stackoverflow.com/questions/48986026
复制相似问题