liveBook · Manning

mapToCanActivate

As explained in Pro Angular 16

mapToCanActivate is an Angular function that converts classes defining canActivate into functional route guards.

mapToCanActivate converts classes that define canActivate into functional route guards for Angular routing. Apply the result to a route's canActivate property when a class-based guard should control activation. section 26.2.2

A class-based activation guard can:

  • Return true to activate a route or false to ignore the navigation request. section 26.2.2
  • Return an Observable<boolean> or Promise<boolean> for asynchronous checks; Angular waits for the result. section 26.2.2
  • Use dependency injection through a registered guard service while handling application-specific navigation decisions. section 26.2.2

Applying a class guard

Register TermsGuard as a service, import mapToCanActivate and TermsGuard in the routing configuration, then assign canActivate: mapToCanActivate([TermsGuard]) to the protected route. In the example, the guard prompts before opening the product-creation form; Yes resolves true and completes navigation, while No resolves false and cancels it. section 26.2.2

How Pro Angular 16 covers it

How deep it goes
Explains class conversion, registration, route application, and boolean or asynchronous results, but defers framework internals.
Examples
Uses TermsGuard, a product-creation confirmation prompt, and a canActivate route configuration.
What it assumes
Assumes familiarity with Angular routing, services, and canActivate while demonstrating class registration and guard results.
What you can do after
Apply it via Angular routing to adapt a class guard and control whether navigation activates a route.
How it connects
Connects route activation with dependency injection, Promise, Observable, and child route guards.

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.