Search code examples
angularangular2-routingangular-routing

How to add CanActivate and loadChildren in the same route


I want to lazy load my module but at the same time protect it using canActivate. I tried:

{ path: 'dashboard/vendor', canActivate: AuthGuard, loadChildren: 'app/module/dashboard/vendor/vendor.module#VendorModule' }

Note that I didn't use children as I have defined the vendor routes in vendor-routing.module using RouterModule.forChild.

But it doesn't work. Any recommendations?


Solution

  • You should use canLoad not canActivate, so if condition is not met it will not load your LazyModule.

    { path: 'dashboard/vendor', canLoad: [AuthGuard], loadChildren: 'app/module/dashboard/vendor/vendor.module#VendorModule' }