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?
canActivate is checked. You navigate between the children of /admin route, but canActivate isn't called because it protects /admin. canActivateChild is called whenever changing between children of the route its defined on.
canActivate is used to prevent unauthorized users from accessing certain routes. See docs for more info. canLoad is used to prevent the application from loading entire modules lazily if the user is not authorized to do so. See docs and example below for more info.
Well, there is a difference, the canActivate exists to prevent unauthorized users from accessing a route, while canLoad is used to prevent the application from loading an entire module or component in a lazy way (lazy loading) if the user is not authorized.
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' }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With