[英]How to check condition based on user role in jhipster typescript file angular 5
I know how to use hasAnyAuthority directive in jhipster for HTML part. 我知道如何在jhipster中将hasAnyAuthority指令用于HTML部分。 But i am searching how can i do this in typescript part like if user has role farmer navigate to farmer profile, for admin navigate to dashboard. 但是我正在搜索如何在打字稿部分中执行此操作,例如如果用户具有角色农夫,请导航到农夫配置文件,而管理员则导航到仪表板。
Use the hasAnyAuthority()
or related functions of Principal
(see principal.service.ts
) in a click handler for instance. 例如,在单击处理程序中使用hasAnyAuthority()
或Principal
相关功能(请参阅principal.service.ts
)。
Also set authorities
property on your route's data according to your needs. 还要根据需要在路线数据上设置authorities
属性。
export const adminState: Routes = [
{
path: '',
data: {
authorities: ['ROLE_ADMIN']
},
canActivate: [UserRouteAccessService],
children: ADMIN_ROUTES
}
];
See JHipster doc about authorizations in Angular: https://www.jhipster.tech/using-angular 有关Angular中的授权,请参阅JHipster文档: https ://www.jhipster.tech/using-angular
Through HTML : 通过HTML:
*jhiHasAnyAuthority="{authorities: 'ROLE_ADMIN', enable:true/false}"
Through TS : 通过TS:
this.principal.identity().then(account => {
if(account.authorities.includes('ROLE_ADMIN')){}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.