Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 路由器可以激活具有多个防护的子级_Angular_Angular Guards - Fatal编程技术网

Angular 路由器可以激活具有多个防护的子级

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

我正在使用多个CanActivate儿童防护装置,如下所示:

{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))
    。通过这种方式,您可以将置换逻辑与激活逻辑分开,这有什么意义吗?