1

Is there any way to load module conditionally or prevent module access in some conditions? I just want to load modules and its subroutes only a specific condition satisfied.

The possible solution I tried till now

  1. Lazy load modules with routing details.
  2. canActivate with auth mechanism service.

Is there any effective way to achieve the same so that.

  1. Common features should not be duplicated.
  2. Module with routing details should be only loaded when in need.

Any help in this regard appreciated TIA.

Mantu Nigam
  • 3,690
  • 7
  • 24
  • 41
  • How is lazy-loading not effective? – JB Nizet Nov 29 '17 at 17:23
  • suppose a set of users does need a module 'X' then there is no need to put that module in the app. It should be only loaded into the memory and browser whenever in need. – Mantu Nigam Nov 29 '17 at 17:25
  • That's exactly what lazy-loading, using the router allows. What is the problem? – JB Nizet Nov 29 '17 at 17:26
  • In this case, as well we need to put entry-level of module X route to the main router module. i.e path: 'lazy', loadChildren: './lazyloadmodule.module#LazyloadmoduleModule' can this entry be conditional as well? – Mantu Nigam Nov 29 '17 at 17:30
  • although the user can navigate to /lazy in such cases. – Mantu Nigam Nov 29 '17 at 17:32
  • I don't understand what you're asking. – JB Nizet Nov 29 '17 at 17:37
  • Like in angular 1.x a module with routing details can be loaded at runtime using some plugin like oclazyload. I just want the similar functionality. – Mantu Nigam Nov 29 '17 at 17:40
  • The router does that. That's what lazy-loading is. If a module is lazy-loaded, then the browser will only load it when the user navigates to one of its routes. – JB Nizet Nov 29 '17 at 17:41
  • Thanks @JB Nizet my problem is that the files can be loaded by invoking that url itself. I just want that should only be loaded with route + some BL in place – Mantu Nigam Nov 29 '17 at 17:46
  • What does "some BL" mean? It seems you want to prevent the browser to work like a browser works, or a webapp to work like a webapp works. If the user goes to a URL, it means he wants to go to that URL. If the module matching that URL is lazy-loaded, then angular should load the module, and navigate to the requested route. Why shouldn't it do that? Not doing that would prevent bookmarking the page, refreshing the page, sending the link of tha page in an email, etc. – JB Nizet Nov 29 '17 at 17:50
  • do you need a combination of the lazy loading and canActivate? I mean do you want a lazy loaded module which is loaded only in case that some condition is true? – GHB Nov 29 '17 at 17:52
  • BL means business logic. Let assume a case a normal user shouldn't have access to admin module. But if he enter admin url in browser the admin module will be loaded in this case. I.e admin.module.js will be loaded. I just want to prevent this – Mantu Nigam Nov 29 '17 at 17:54
  • @GHB you got me right thanks – Mantu Nigam Nov 29 '17 at 17:55
  • Ah. Then use a CanLoad guard. Not a CanActivate. – JB Nizet Nov 29 '17 at 17:57
  • you can do many things. in the routing of main component of your lazy loaded module, you can put a canActivate guard. you can also check your conditions in it's initial level and navigate away if they dont match. and best of all, use a CanLoad guard. – GHB Nov 29 '17 at 17:58
  • Thanks @GHB does can load guard prevent loading of module file if condition is not satisfied? – Mantu Nigam Nov 29 '17 at 18:01
  • yes, that's it's purpose. from the docs: "Interface that a class can implement to be a guard deciding if a children can be loaded." https://angular.io/api/router/CanLoad – GHB Nov 29 '17 at 18:10

1 Answers1

2

check the CanLoad guard.

Interface that a class can implement to be a guard deciding if a children can be loaded.

GHB
  • 917
  • 6
  • 13