【问题标题】:Different RoleGuard in a Child Item Angular Routing子项角度路由中的不同 RoleGuard
【发布时间】:2019-10-19 05:43:08
【问题描述】:

我想知道您是否使用 RoleGuard 来检查是否有人可以激活路径,并且您希望在子项目中使用不同的 RoleGuard 是可能的:

我已经尝试过了,但我无法使用不同的 RoleGuard 访问路径

{
    path: 'admin',
    canActivate: [AdminGuard],
    children: [
        { path: '', redirectTo: 'test', pathMatch: 'full'},
        { path: 'test', component: MasterDataComponent},
        { path: 'test/subtest', component: ObjectsTypeComponent },
        { path: 'test/subtest/:operation', component: ObjectsTypeDetailComponent },
        { path: 'test/subtest/:operation/:id', component: ObjectsTypeDetailComponent },
        { path: 'test/money', component: DivisesComponent, canActivate: [OperatorGuard] } 
}

所以,只有管理员可以进入此路径,但我想允许操作员进入路径 test/money。

感谢您的建议。

【问题讨论】:

  • 然后更改 AdminGuard 的实现或为 Operator 添加另一个保护。

标签: angular typescript angular-routing angular-guards


【解决方案1】:

你试图在已经有 Guard 的路由中使用 Guard。如果守卫“增强安全性”(也就是添加属性),那就没问题了。

但是,一旦你想覆盖保护属性,你有两个选择:

  1. 分别为每条路线添加警卫。
  2. 添加另一个将覆盖第一个路由的路由(您必须编写OperatorOrAdminGuard)。
{
    path: 'admin',
    canActivate: [AdminGuard],
    children: [
            { path: '', redirectTo: 'test', pathMatch: 'full'},
            { path: 'test', component: MasterDataComponent},
            { path: 'test/subtest', component: ObjectsTypeComponent },
            { path: 'test/subtest/:operation', component: ObjectsTypeDetailComponent },
            { path: 'test/subtest/:operation/:id', component: ObjectsTypeDetailComponent },
    ],
},
{
    path: 'admin/test/money',
    component: DivisesComponent, 
    canActivate: [OperatorOrAdminGuard]
}

您可以使用GuardFactory,而不是为不同的用户创建多个保护。这里解释一下:Pass parameter into route guard

然后,像这样使用它:

{ 
   path: 'admin/test/money, 
   component: DivisesComponent,
   canActivate: RoleGuard,
   data: {roles: ['Operator', 'Admin']}
}

【讨论】:

  • 很高兴为您提供帮助。 :D
猜你喜欢
  • 2014-07-19
  • 1970-01-01
  • 1970-01-01
  • 2021-04-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-12
  • 1970-01-01
相关资源
最近更新 更多