liveBook · Manning

Child route guard

As explained in Pro Angular 16

A child route guard is a guard that controls activation of the child routes belonging to a parent route.

A child route guard controls activation for the child routes belonging to a parent route. Angular invokes it before activating any child, using route snapshots and a boolean, Observable, or Promise result. section 26.2.2

Use a child guard when several child routes share an activation rule:

  • Define a function receiving ActivatedRouteSnapshot and RouterStateSnapshot, or prefer a class with canActivateChild for dependency injection. section 26.2.2
  • Apply the guard to the parent route through canActivateChild and mapToCanActivateChild; Angular then checks each child navigation. section 26.2.2
  • Return true to activate a child and false to cancel navigation; asynchronous checks resolve through an Observable or Promise. section 26.2.2

Guarding product-management children

A parent route containing products and categories can set canActivateChild to mapToCanActivateChild([TermsGuard]). TermsGuard can inspect the requested route, prompt through a message service, and allow or reject activation. The guard can protect only the categories child route while immediately allowing other children. section 26.2.2

How Pro Angular 16 covers it

How deep it goes
Explains parent-route configuration, guard implementations, snapshots, dependency injection, and synchronous or asynchronous results.
Examples
Uses products and categories child routes, TermsGuard, a confirmation prompt, and a message service.
What it assumes
Assumes familiarity with parent and child routes, while refreshing route snapshots, guard results, and dependency-injected guard classes.
What you can do after
Configure Angular Router child activation checks that allow, reject, or asynchronously decide navigation.
How it connects
Connects child route guards with route guards, canActivate, dependency injection, routing configuration, and navigation events.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Pro Angular 16. The section links point to the parts of the book it draws on.