CanLoad

Stable

Interface

What it does

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

How to use

  1. class UserToken {}
  2. class Permissions {
  3. canLoadChildren(user: UserToken, id: string): boolean {
  4. return true;
  5. }
  6. }
  7. @Injectable()
  8. class CanLoadTeamSection implements CanLoad {
  9. constructor(private permissions: Permissions, private currentUser: UserToken) {}
  10. canLoad(route: Route(
  11. route: Route
  12. ): Observable<boolean>|Promise<boolean>|boolean {
  13. return this.permissions.canLoadChildren(this.currentUser, route);
  14. }
  15. }
  16. @NgModule({
  17. imports: [
  18. RouterModule.forRoot([
  19. {
  20. path: 'team/:id',
  21. component: TeamCmp,
  22. loadChildren: 'team.js',
  23. canLoad: [CanLoadTeamSection]
  24. }
  25. ])
  26. ],
  27. providers: [CanLoadTeamSection, UserToken, Permissions]
  28. })
  29. class AppModule {}

You can alternatively provide a function with the canLoad signature:

  1. @NgModule({
  2. imports: [
  3. RouterModule.forRoot([
  4. {
  5. path: 'team/:id',
  6. component: TeamCmp,
  7. loadChildren: 'team.js',
  8. canLoad: ['canLoadTeamSection']
  9. }
  10. ])
  11. ],
  12. providers: [
  13. {
  14. provide: 'canLoadTeamSection',
  15. useValue: (route: Route) => true
  16. }
  17. ]
  18. })
  19. class AppModule {}

Interface Overview

interface CanLoad {
canLoad(route: Route) : Observable<boolean>|Promise<boolean>|boolean

}

Interface Description

Interface Details

exported from @angular/router/index, defined in @angular/router/src/interfaces.ts