Angular 执行多用户角色检查

Angular 执行多用户角色检查,angular,jwt,Angular,Jwt,我需要为用户角色为1或2的用户访问UserComponent。它在批准模块中显示为(数据:[1,2]}) 从CanActivateGuard服务中,我应该如何执行此检查以允许用户使用UserRole1或2。目前我的代码是this.jwtHelperService.decodeToken(“”)。role===router.data。但是我需要检查两个用户角色(1和2)是否都被允许 批准模块 const routes:routes=[ ... {路径:“用户”,组件:UserComponent,c

我需要为用户角色为
1或2的用户访问
UserComponent
。它在
批准模块中显示为(
数据:[1,2]}

CanActivateGuard
服务中,我应该如何执行此检查以允许用户使用
UserRole
1或2。目前我的代码是this.jwtHelperService.decodeToken(“”)。role===router.data
。但是我需要检查两个用户角色(1和2)是否都被允许

批准模块

const routes:routes=[
...
{路径:“用户”,组件:UserComponent,canActivate:[CanActivateGuard],数据:[1,2]},
...
];
CanActivateGuard

canActivate(路由器:ActivatedRouteSnapshot):布尔值
{  
var token=localStorage.getItem(“token”)?localStorage.getItem(“token”):null;
if(this.jwtHelperService.decodeToken(“令牌”).role===router.data)
{
返回true;
}
}

有很多方法可以继续,接近您的逻辑并使用您可以执行以下操作:

canActivate(路由器:ActivatedRouteSnapshot):布尔值
{  
var token=localStorage.getItem(“token”)?localStorage.getItem(“token”):null;
//您可以执行此附加检查,以避免下面出现空指针异常
if(token==null)返回false;
//如果解码令牌的角色为1或2,则它将通过此检查
if(router.data.includes(this.jwtHelperService.decodeToken(token.role))
{
返回true;
}
返回false;
}

有很多方法可以继续,接近您的逻辑并使用您可以执行以下操作:

canActivate(路由器:ActivatedRouteSnapshot):布尔值
{  
var token=localStorage.getItem(“token”)?localStorage.getItem(“token”):null;
//您可以执行此附加检查,以避免下面出现空指针异常
if(token==null)返回false;
//如果解码令牌的角色为1或2,则它将通过此检查
if(router.data.includes(this.jwtHelperService.decodeToken(token.role))
{
返回true;
}
返回false;
}