How to apply canActivate guard on all the routes?

前端 未结 3 1395
慢半拍i
慢半拍i 2020-12-12 20:29

I have a angular2 active guard which handle if the user is not logged in, redirect it to login page:

import { Injectable } from  \"@angular/core\";
import {          


        
3条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-12 20:51

    You can introduce a componentless parent route and apply the guard there:

    const routes: Routes = [
        {path: '', canActivate:[AuthenticationGuard], children: [
          { path : '', component: UsersListComponent },
          { path : 'add', component : AddComponent},
          { path : ':id', component: UserShowComponent },
          { path : 'delete/:id', component : DeleteComponent },
          { path : 'ban/:id', component : BanComponent },
          { path : 'edit/:id', component : EditComponent }
        ]}
    ];
    

提交回复
热议问题