Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 882
czetsuya Avatar asked Sep 23 '17 14:09

czetsuya


People also ask

Can activate VS can activate child?

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.

Can activate vs CanLoad?

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.

What is canActivate and Candeactivate?

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.


1 Answers

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' }
like image 54
alexKhymenko Avatar answered Nov 03 '22 02:11

alexKhymenko