CanActivateChild

Stable

Interface

What it does

Interface that a class can implement to be a guard deciding if a child route can be activated.

How to use

  1. class UserToken {}
  2. class Permissions {
  3. canActivate(user: UserToken, id: string): boolean {
  4. return true;
  5. }
  6. }
  7. @Injectable()
  8. class CanActivateTeam implements CanActivateChild {
  9. constructor(private permissions: Permissions, private currentUser: UserToken) {}
  10. canActivateChild(
  11. route: ActivatedRouteSnapshot,
  12. state: RouterStateSnapshot
  13. ): Observable<boolean>|Promise<boolean>|boolean {
  14. return this.permissions.canActivate(this.currentUser, route.params.id);
  15. }
  16. }
  17. @NgModule({
  18. imports: [
  19. RouterModule.forRoot([
  20. {
  21. path: 'root',
  22. canActivateChild: [CanActivateTeam],
  23. children: [
  24. {
  25. path: 'team/:id',
  26. component: Team
  27. }
  28. ]
  29. }
  30. ])
  31. ],
  32. providers: [CanActivateTeam, UserToken, Permissions]
  33. })
  34. class AppModule {}

You can alternatively provide a function with the canActivateChild signature:

  1. @NgModule({
  2. imports: [
  3. RouterModule.forRoot([
  4. {
  5. path: 'root',
  6. canActivateChild: ['canActivateTeam'],
  7. children: [
  8. {
  9. path: 'team/:id',
  10. component: Team
  11. }
  12. ]
  13. }
  14. ])
  15. ],
  16. providers: [
  17. {
  18. provide: 'canActivateTeam',
  19. useValue: (route: ActivatedRouteSnapshot, state: RouterStateSnapshot) => true
  20. }
  21. ]
  22. })
  23. class AppModule {}

Interface Overview

interface CanActivateChild {
canActivateChild(childRoute: ActivatedRouteSnapshot, state: RouterStateSnapshot) : Observable<boolean>|Promise<boolean>|boolean

}

Interface Description

Interface Details

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