How to hide link by guard angular2

前端 未结 2 2052
我寻月下人不归
我寻月下人不归 2021-02-08 10:45

How to hide admin link in view html. I have to guard :Admin and Manager

Router config :

{
   path: \'manager\',
   component: ManagerComponent,
   canAct         


        
2条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-08 11:42

    In my opinion this has nothing to do with the router itself.

    Your Guards may call another service which has the information about which kind of user is logged in: Admin or Manager.

    You should create a service that knows the type of the user. Then inject this service with Dependency Injection into your component where you have the routerLink.

    There you can ask the service and toggle the link with *ngIf:

  • User
  • So the service provides a function that gives you the user which is currently logged in and the isAdmin()-function of the user returns true or false. If the user is admin then the

  • will be shown, otherwise it will be hidden.

    This is just an example but I hope you get the point. It has more to do with basic functionality of Angular 2 rather than the router of Angular 2. More information about *ngIf can be found here.

提交回复
热议问题