{ "id": "api/router/CanActivateChild", "title": "CanActivateChild", "contents": "\n\n
\n
\n
\n \n API > @angular/router\n
\n \n
\n \n
\n

CanActivateChildlink

\n \n \n \n \n \n
\n \n \n\n
\n \n
\n

Interface that a class can implement to be a guard deciding if a child route can be activated.\nIf all guards return true, navigation continues. If any guard returns false,\nnavigation is cancelled. If any guard returns a UrlTree, current navigation\nis cancelled and a new navigation begins to the UrlTree returned from the guard.

\n\n

See more...

\n
\n \n \n
\n\ninterface CanActivateChild {\n canActivateChild(childRoute: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree\n}\n\n\n \n \n\n\n \n \n\n
\n\n \n\n \n \n
\n

Descriptionlink

\n

The following example implements a CanActivateChild function that checks whether the\ncurrent user has permission to activate the requested child route.

\n\nclass UserToken {}\nclass Permissions {\n canActivate(user: UserToken, id: string): boolean {\n return true;\n }\n}\n\n@Injectable()\nclass CanActivateTeam implements CanActivateChild {\n constructor(private permissions: Permissions, private currentUser: UserToken) {}\n\n canActivateChild(\n route: ActivatedRouteSnapshot,\n state: RouterStateSnapshot\n ): Observable<boolean|UrlTree>|Promise<boolean|UrlTree>|boolean|UrlTree {\n return this.permissions.canActivate(this.currentUser, route.params.id);\n }\n}\n\n

Here, the defined guard function is provided as part of the Route object\nin the router configuration:

\n\n@NgModule({\n imports: [\n RouterModule.forRoot([\n {\n path: 'root',\n canActivateChild: [CanActivateTeam],\n children: [\n {\n path: 'team/:id',\n component: TeamComponent\n }\n ]\n }\n ])\n ],\n providers: [CanActivateTeam, UserToken, Permissions]\n})\nclass AppModule {}\n\n

You can alternatively provide an in-line function with the canActivateChild signature:

\n\n@NgModule({\n imports: [\n RouterModule.forRoot([\n {\n path: 'root',\n canActivateChild: ['canActivateTeam'],\n children: [\n {\n path: 'team/:id',\n component: TeamComponent\n }\n ]\n }\n ])\n ],\n providers: [\n {\n provide: 'canActivateTeam',\n useValue: (route: ActivatedRouteSnapshot, state: RouterStateSnapshot) => true\n }\n ]\n})\nclass AppModule {}\n\n\n \n
\n\n \n\n \n\n
\n

Methodslink

\n \n \n\n \n \n \n \n \n \n \n \n\n \n\n \n \n
\n
\n

\n canActivateChild()\n \n link

\n \n
\n
\n
\n \n\n canActivateChild(childRoute: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree\n\n \n\n
Parameters
\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
\n \n childRoute\n ActivatedRouteSnapshot\n \n \n
\n \n state\n RouterStateSnapshot\n \n \n
\n\n \n
Returns
\n

Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree

\n\n \n\n\n \n\n \n
\n
\n\n \n
\n\n\n \n\n\n
\n
\n\n\n" }