简体   繁体   中英

canActivateChild is called multiple times

In my angular 5 project I "protect" some views based on the logged user role, so in my routing module I have:

    path: '',
    component: AdminLayoutComponent,
    canActivate: [AuthGuard],
    canActivateChild: [RoleGuard],
    children: [
      {
        path: '',
        loadChildren: './dashboard/dashboard.module#DashboardModule',
        data: { roles: ['role.all'] }
      },
      {
        path: 'checkPoints',
        loadChildren: './check-points/check-point-list/check-point-list.module#CheckpointListModule',
        data: { roles: ['role.admin', 'role.tech', 'role.backoffice'] }
      }...

AuthGuard check if the user is authenticated, and then RoleGuard check if the logged user has the role to see the page. But I see the canActivateChild method called multiple times when I try to navigate into child routes (2-3 times)

Is it correct behaviour?

{
        path: "",
        component: AdminLayoutComponent,
        canActivate: [AuthGuard],
        children: [
            {
                path: "",
                canActivate: [RoleGuard],
                children: [
                    {
                      path: '',
                      loadChildren: './dashboard/dashboard.module#DashboardModule',
                      data: { roles: ['role.all'] }
      },...

i think this way would prevent 2-3 times.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM