我有一个使用 Angular 4 开发的现有项目。我需要根据用户权限控制对特定路由的访问。简化的路由配置如下所示:
[
{ path: '', redirectTo: '/myApp/home(secondary:xyz)', pathMatch: 'full' },
{ path: 'myApp'
children: [
{ path: '', redirectTo: 'home', pathMatch: 'full' },
{ path: 'home', ... },
...
{ path: 'product'
children: [
{ path: '', redirectTo: 'categoryA', pathMatch: 'full' },
{ path: 'categoryA', component: CategoryAComponent, canActivate: [CategoryACanActivateGuard]},
{ path: 'categoryB', component: CategoryBComponent},
...
]
},
...
]
},
...
]
Run Code Online (Sandbox Code Playgroud)
现在,我想控制对www.myWeb.com/myApp/product/categoryA. 如果用户没有足够的权限,他/她将被重定向到... /product/CategoryB。我已经写了一个CanActivateRouteGuard 来做到这一点,守卫类看起来像这样:
import { Injectable } from …Run Code Online (Sandbox Code Playgroud)