[英]Angular Role Based Menu And Page Routing
I have 3 roles.我有3个角色。 Admin, Customer, and Employee.
管理员、客户和员工。 For example, when someone with username: aa@hotmail.com and password: 123 roles:[admin,customer] logs into my site, he should see (Home,Customers and business) pages in the menu.
例如,当用户名:aa@hotmail.com 和密码:123 角色:[admin,customer] 登录我的站点时,他应该会在菜单中看到(主页、客户和业务)页面。 When another user with the customer role logs in, they should see (Home,Customers) pages.
当另一个具有客户角色的用户登录时,他们应该会看到 (Home,Customers) 页面。 To summarize, how can I do role-based menu management?
总而言之,如何进行基于角色的菜单管理? Can you give an example or help?
你能举个例子或帮助吗?
You can easily check if the current user has a certain role in his roles
variable by using includes
(assuming the roles
variable is an array).您可以使用
includes
轻松检查当前用户在其roles
变量中是否具有特定角色(假设roles
变量是一个数组)。 Then using *ngIf
on your template you can enable or not the pages on your menu.然后在您的模板上使用
*ngIf
您可以启用或不启用菜单上的页面。
Here is a stackblitz with a little example.这是一个带有小例子的stackblitz。
Moreover, be sure to use canActivate
interface on your routes to avoid any possibility to go to any admin page without admin account.此外,请务必在您的路线上使用
canActivate
接口,以避免 go 到没有管理员帐户的任何管理页面的任何可能性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.