admin管理员组

文章数量:1287517

I know that we can group routes located in one module. Like that:

canActivate: [AuthGuard],
    children: [
      {
        path: '',
        children: [
          { path: 'crises', ponent: ManageCrisesComponent },
          { path: 'heroes', ponent: ManageHeroesComponent },
          { path: '', ponent: AdminDashboardComponent }
        ],
      }

But I should add that guard to each module's routing file. And I have many of them.

I want that the user can not go to any route except one (login route) if he is not authorized.

What is the right way to add guard to all of them??

I know that we can group routes located in one module. Like that:

canActivate: [AuthGuard],
    children: [
      {
        path: '',
        children: [
          { path: 'crises', ponent: ManageCrisesComponent },
          { path: 'heroes', ponent: ManageHeroesComponent },
          { path: '', ponent: AdminDashboardComponent }
        ],
      }

But I should add that guard to each module's routing file. And I have many of them.

I want that the user can not go to any route except one (login route) if he is not authorized.

What is the right way to add guard to all of them??

Share Improve this question edited Nov 29, 2016 at 18:41 Sergey Tyupaev asked Nov 29, 2016 at 18:23 Sergey TyupaevSergey Tyupaev 1,27310 silver badges24 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 12

You can use a ponentless empty path parent route with the guard

{ path: '', canActivate: [AuthGuard],  children: [
  {
    path: '',
    children: [
      { path: 'crises', ponent: ManageCrisesComponent },
      { path: 'heroes', ponent: ManageHeroesComponent },
      { path: '', ponent: AdminDashboardComponent }
    ],
  }
}

and in the guard check if the user is logged in. If not logged in and the current route is login then still allow it.

本文标签: javascriptAngular2 CanActivate guard for all routes except oneStack Overflow