有条件地重定向到子路由

时间:2019-01-29 07:38:02

标签: angular router

在一种情况下,我有一个懒惰加载的子路由要有条件地显示:

const routes: Routes = [
  {
    path: '',
    component: ParentComponent,
    canActivate: [ParentGuard],
    children: [
      {
        path: 'child1', loadChildren:'./child1/child1.module#Child1Module'
      },
     {
        path: 'child2', loadChildren:'./child2/child2.module#Child2Module'
      },
    ]
  }
];

我从如下页面导航到ParentComponent

this.router.navigate('app/parent');

现在的问题是,我必须根据某些情况显示 child1 child2 组件。

为此,我添加了一个守卫,

      canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot){
    if (this.isAdmin) {
      this.router.navigate(['app/parent/child1']);
      return false;
    } else {
      this.router.navigate(['app/parent/child2']);
return false;
    }
    return true;
      }

但是,这导致此防护措施被反复调用,并且我的应用程序挂起。我该怎么解决?

0 个答案:

没有答案
相关问题