Angular 中是否有 canActivate 的选项/变量 4

Are there options / variables for canActivate in Angular 4

我想根据用户拥有的权限拒绝访问路线。所以我在我的 mongoDb 中有一个 table,实际上有 userRights 和两个简单​​的守卫。一个用于登录用户,一个用于管理员。

所以现在我想给用户访问特定路线的权利。因此,我想添加我的 canActivate: [checkForRightGuard] ,它在右边被解析,所以检查一下,但我该怎么做?

我想到这个解决方案是因为我不认为我必须为我必须检查的每项权利创建一个守卫 - 或者?

所以我考虑了一下。像这样(当然,这是行不通的,但我认为它显示了我所需要的)

{path: 'admin/expandSoftware', component: ExpandSoftwareComponent, canActivate: [AdminGuard('canExpandSoftware')]},

您可以将“数据”属性 添加到路由中,如下所示:-

{
    path: 'admin/expandSoftware', 
    component: ExpandSoftwareComponent, 
    canActivate: [AdminGuard],
    data: {authorization: 'canExpandSoftware'}
}

然后在你的路由守卫中,你可以按如下方式访问它:-

canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
    console.log(route.data["authorization"]);
    return this.canActivate(route, state);
}

but be careful you may not be allowed to pass dynamic (run time) data to your routes if you are using AOT (Ahead of Time) compilation, as the routes are analyzed at compile time when using AOT compilation.