gpt4 book ai didi

Angular 2 如何在同一路径上应用多个具有或关系的守卫

转载 作者:太空狗 更新时间:2023-10-29 17:01:37 24 4
gpt4 key购买 nike

我的应用程序允许根据用户 Angular 色访问内容。我为每个 Angular 色写了一个 Router Guard。某些内容允许 role1 或 role2 或 role3 访问。我应该如何在 feature-routing.module.ts 文件中编写 canActivate 声明?据我了解,如果我写

canActivate:[Role1Guard, Role2Guard, Role3Guard]

如果任何守卫返回 false,访问将被拒绝。但就我而言,如果任何守卫返回 true,我应该允许访问。怎么做?非常感谢!

最佳答案

在这种情况下,我们可以做的是创建一个 Master Guard,它将根据我们的要求触发应用程序防护。

结帐 this回答以了解该方法。

假设您已经通过上面的链接,在这种情况下,方法可以像修改 Route 类中的 data 属性一样简单。

像这样的——

{
path: "view2",
component: View2Component,
//attach master guard here
canActivate: [MasterGuard],
//this is the data object which will be used by
//masteer guard to execute guard1, guard 2, guard 3 & guard 4
data: {
trigger: {
operator: "OR",
guards: [
GUARDS.GUARD1,
GUARDS.GUARD2,
GUARDS.GUARD3,
GUARDS.GUARD4
]
}
}
}

然后使用 operator 标志相应地触发所有守卫。

希望对您有所帮助:)

关于Angular 2 如何在同一路径上应用多个具有或关系的守卫,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43163042/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com