3

Before Angular 15, I used SCAM pattern in order to have one module per component/directive/pipe.

My app contains many lazy loaded pages (one module per page) with a CanLoad guard to prevent lazy loading (feature flags, ...).

Since Ng 15 (I'm on 15.0.4), I replaced all of my loadChildren by loadComponent (so, each pages became a standalone component). But... As I saw there :

CanLoad guards do not apply to loadComponent. canActivate should be used instead, just like you would do if it were simply

Problem : CanActivate doesn't prevent loading if needed. So now, all of my pages are loaded :(.

Is there a way to keep the "CanLoad" functionnality, but for loadComponent ?

Thanks !

EDIT : I tried with "canMatch" : It doesn't change anything.

Adrien SAULNIER
  • 1,651
  • 1
  • 13
  • 19

1 Answers1

1

I'm quoting the source code here :

  // Note that `canLoad` is only checked as a condition that prevents `loadChildren` and not
  // `loadComponent`. `canLoad` guards only block loading of child routes by design. This
  // happens as a consequence of needing to descend into children for route matching immediately
  // while component loading is deferred until route activation. Because `canLoad` guards can
  // have side effects, we cannot execute them here so we instead skip preloading altogether
  // when present. Lastly, it remains to be decided whether `canLoad` should behave this way
  // at all. Code splitting and lazy loading is separate from client-side authorization checks
  // and should not be used as a security measure to prevent loading of code.

Source

So don't expect canLoad to working with loadComponent().

canMatch() should be working though.

Matthieu Riegler
  • 31,918
  • 20
  • 95
  • 134