'Angular router uses wrong guard

I have a problem with guard in routing module. The issue is that the guard is triggered on every route of lazy loaded module while it should not.

From the app-routing.module.ts :

  {
    path: '',
    children: [],
    canActivate: [RedirectGuard],
    pathMatch: 'full',
  },
  {
    path: 'x',
    loadChildren: () => import('./xmodule/xmodule.module').then(m => m.xModule)
  }

and in x module:

  {
    path: '',
    component: MyComponent,
    canActivate: [PermissionGuard],
    data: {
      roles: [
        Role.TEST_ROLE
      ]
    }
  },

The problem is that RedirectGuard runs on every route, so basically when I enter localhost:4200 - it runs, but when I enter localhost:4200/x it also runs.

Can someone help?

EDIT:

I have checked and even on basic setup like:

  {
    path: 'testing',
    component: TestingComponent
  },
  {
    path: '',
    redirectTo: '/testing',
    pathMatch: 'full'
  },

in app-routing.module there is something wrong. When I type localhost:4200 it redirects me to localhost:4200/testing, but then when I refresh page it is redirecting me to: localhost:4200/testing/testing, where it should stay on localhost:4200/testing....



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source