Full answer
A navigation is a pipeline of steps. Knowing the order helps you place logic in the right guard.
- Trigger:
routerLink,router.navigate, or a URL change. The router emitsNavigationStart. - Recognition: the URL is parsed into a tree and matched against the route config. During matching,
canMatchguards run for each candidate route. A false result means "this route does not match", and the router tries the next route. This is also where redirects (redirectTo) apply, and lazyloadChildrenorloadComponentbundles load. canDeactivateguards of the components being left run, starting from the deepest (they can warn about unsaved changes).canActivateChildof parents, andcanActivateof each route being entered, run from the top route down to the child. If any returns false, the navigation is cancelled. If any returns aUrlTree, the router cancels and redirects.- Resolvers run (in parallel for sibling routes) and their results are stored in
route.data. If a resolver errors, the navigation fails (NavigationError) unless you handle it. canLoadis the older name of the check before loading a lazy module (replaced bycanMatch).- Activation: components are created or reused,
router-outlets display them, and titles are set. The router emitsNavigationEnd, andScrollevents follow.
Events you can watch: NavigationStart, RoutesRecognized, GuardsCheckStart, GuardsCheckEnd, ResolveStart, ResolveEnd, NavigationEnd, NavigationCancel, NavigationError.
Guideline for placement:
- Choose the route based on a condition (feature flag, role):
canMatch. - Block or redirect based on authentication:
canActivate. - Prevent losing edits:
canDeactivate. - Load data the page cannot render without: resolver (or load in the component with a loading state, which is often a better experience).
If a newer navigation starts while one is running, the older one is cancelled (NavigationCancel), so guards and resolvers should return observables that are safe to unsubscribe.
typescript
import { inject } from '@angular/core';
import { CanActivateFn, CanDeactivateFn, CanMatchFn, ResolveFn, Routes } from '@angular/router';
import { Component } from '@angular/core';
export const order: string[] = [];
const canMatch: CanMatchFn = () => (order.push('canMatch'), true);
const canActivate: CanActivateFn = () => (order.push('canActivate'), true);
const resolve: ResolveFn<string> = () => (order.push('resolve'), 'data');
@Component({ selector: 'app-page-a', template: `A` })
export class PageAComponent {}
@Component({ selector: 'app-page-b', template: `B` })
export class PageBComponent {}
export const canLeave: CanDeactivateFn<PageAComponent> = () => (order.push('canDeactivate'), true);
export const routes: Routes = [
{ path: 'a', component: PageAComponent, canDeactivate: [canLeave] },
{ path: 'b', component: PageBComponent, canMatch: [canMatch], canActivate: [canActivate], resolve: { data: resolve } },
];
// Navigating from /a to /b records (captured order, see the notes):
// canMatch, canDeactivate, canActivate, resolve (captured in Karma, Angular 20.3)Angular 20.3, type-checked. The order in the comment below was captured from a Karma run in headless Chrome with logging guards and a resolver.
Key points
- Order: recognise and match (`canMatch`), `canDeactivate`, `canActivateChild`/`canActivate` top down, resolvers, activation.
- A `false` result cancels navigation. A `UrlTree` redirects.
- Events mark each stage and can drive loading indicators.
- A new navigation cancels the previous one.
Common mistakes
- Putting role-based route selection in `canActivate` when `canMatch` fits and avoids loading the code.
- Doing slow work in a resolver and blocking navigation with no feedback.
- Not handling resolver errors.
Follow-ups
- Why is `canMatch` better than `canActivate` for feature flags?
- Do resolvers for parent and child routes run in sequence or in parallel?
- How do you prevent a user from leaving a dirty form?
Tips
- Walk through the stages in order. Interviewers expect the sequence.
- Name the events for each stage.
- Give one example guard for each type.
Evaluation rubric
- Gets the order of the stages right.
- Knows which guard fits which problem.
- Understands cancellation.