当我在Angular 2中使用guard children route时,浏览器完全被阻止

当我在Angular 2中使用guard children route时,浏览器完全被阻止,angular,angular2-routing,guard,canactivate,Angular,Angular2 Routing,Guard,Canactivate,当我在我的“pre-harvest”子路径中使用RoleGuard并打开浏览器时,浏览器被完全阻塞,似乎处于无限循环中。我没有任何编译问题,也无法打开控制台查看我有哪些错误 可以在子路径中使用canActivate吗?还是应该使用CanActivateChild?我对CanActivateChild没有这个问题 const preHarvestRoutes: Routes = [ { path: '', component: PrivateComponent, can

当我在我的“pre-harvest”子路径中使用RoleGuard并打开浏览器时,浏览器被完全阻塞,似乎处于无限循环中。我没有任何编译问题,也无法打开控制台查看我有哪些错误

可以在子路径中使用canActivate吗?还是应该使用CanActivateChild?我对CanActivateChild没有这个问题

const preHarvestRoutes: Routes = [
  {
    path: '',
    component: PrivateComponent,
    canActivate: [AuthGuard],
    children: [
      {
        path: 'pre-harvest',
        component: PreHarvestComponent,
        canActivate: [RoleGuard],  <------- IF I REMOVE THIS I DO NOT HAVE ANY PROBLEM.
        children: [
          {
            path: 'new-field',
            component: NewFieldComponent
          },
        ]
      }
    ]
  }
];
const preHarvestRoutes:Routes=[
{
路径:“”,
组件:私有组件,
canActivate:[AuthGuard],
儿童:[
{
路径:“收获前”,
组件:采集前组件,

canActivate:[RoleGuard],您正在重定向到与放置
RoleGuard
的路径相同的路径。这显然会导致无限循环。您应该将
RoleGuard
更改为:

canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
   //let roles = route.data['roles'] as Array<string>;
   //let rolesUserLogged = webStorage.user;
   return true;
}
canActivate(路由:ActivatedRouteSnapshot,状态:RouterStateSnashot):布尔值{
//让roles=route.data['roles']作为数组;
//让rolesUserLogged=webStorage.user;
返回true;
}

您仍然需要指定RoleGuard逻辑,但您遇到的问题是重定向到同一路由。

以回答您的最后一个问题。可以激活当前路由及其所有子路由的句柄访问权限。可以激活仅对当前路由的子路由的句柄访问权限,而不是对路由本身的句柄访问权限。Doc:您应该添加y代码我们的
RoleGuard
查看问题所在is@PierreDuc我添加了角色守卫代码。
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
   //let roles = route.data['roles'] as Array<string>;
   //let rolesUserLogged = webStorage.user;
   return true;
}