Sometimes, for security reasons, we do not want the user to be able to even see the source code of the lazily loaded bundle if she does not have the right permissions. That's what the
canLoad
guard is for. If a canLoad
guard returns false, the router will not load the bundle.
Let's take this configuration from the previous chapter and set up a
canLoad
guard to restrict access to contacts:
const ROUTES = [ { path: ':folder', children: [ { path: '', component: ConversationsCmp }, { path: ':id', component: ConversationCmp, children: [...] } ] }, { path: 'contacts', canLoad: [CanLoadContacts], loadChildren: 'contacts.bundle.js' } ];
Where the CanLoadContacts
class is defined like this:
@Injectable() class CanLoadContacts implements CanLoad { constructor(private...