Angular 路由器可以激活具有多个防护的子级
我正在使用多个CanActivate儿童防护装置,如下所示:Angular 路由器可以激活具有多个防护的子级,angular,angular-guards,Angular,Angular Guards,我正在使用多个CanActivate儿童防护装置,如下所示: {path: 'admin', canActivateChild : [AdminAuthGuard, EmployeeAuthGuard], children: adminRoutes } 以下是管理员保护的定义: canActivateChild() : boolean { const role = localStorage.getItem('role'); if(role === 'admin') { r
{path: 'admin', canActivateChild : [AdminAuthGuard, EmployeeAuthGuard], children: adminRoutes }
以下是管理员保护的定义:
canActivateChild() : boolean {
const role = localStorage.getItem('role');
if(role === 'admin') {
return true;
}
else {
return false;
}
}
我有一个非常相似的员工警卫,我在那里检查作为员工的角色。
上述代码的问题是:
实际代码要比这复杂得多。这只是获取帮助的示例代码。在检查两个返回值的其他两个保护之外创建一个组合保护:
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 }
灵感来源于@Safiyya可能的复制品,我不想检查所有的守卫。我的应用程序中有10个GURD。使用共享服务将检查每个警卫。对于管理员路线,我只想检查两个守卫。是否还有其他解决方案?这似乎是一个解决方案,但在这里我需要创建很多组合守卫,因为我必须在这里进行很多排列?您可以使用一个工厂,为每个案例返回
Array
,并在组合守卫中返回Array.map(g=>g.canActivate(r,s))
。通过这种方式,您可以将置换逻辑与激活逻辑分开,这有什么意义吗?