Pass parameter into route guard

后端 未结 3 1878
暖寄归人
暖寄归人 2020-12-02 12:28

I\'m working on an app that has a lot of roles that I need to use guards to block nav to parts of the app based on those roles. I realize I can create individual guard class

3条回答
  •  孤街浪徒
    2020-12-02 13:13

    Instead of using forRole(), you can do this:

    { 
       path: 'super-user-stuff', 
       component: SuperUserStuffComponent,
       canActivate: RoleGuard,
       data: {roles: ['SuperAdmin', ...]}
    }
    

    and use this in your RoleGuard

    canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot)
        : Observable | Promise | boolean  {
    
        let roles = route.data.roles as Array;
        ...
    }
    

提交回复
热议问题