← Back to Angular, page 7

angular-0129 · Medium mid

What happens, step by step, when a user navigates to a URL in the Angular router, and in what order do guards and resolvers run?

The router parses the URL, matches routes (running canMatch), redirects, then runs canDeactivate on the routes being left, then canActivateChild and canActivate from parent to child, then resolvers, and finally activates the components. Any guard that returns false or a UrlTree cancels the navigation.

Full answer

A navigation is a pipeline of steps. Knowing the order helps you place logic in the right guard.

  1. Trigger: routerLink, router.navigate, or a URL change. The router emits NavigationStart.
  2. Recognition: the URL is parsed into a tree and matched against the route config. During matching, canMatch guards 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 lazy loadChildren or loadComponent bundles load.
  3. canDeactivate guards of the components being left run, starting from the deepest (they can warn about unsaved changes).
  4. canActivateChild of parents, and canActivate of each route being entered, run from the top route down to the child. If any returns false, the navigation is cancelled. If any returns a UrlTree, the router cancels and redirects.
  5. 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.
  6. canLoad is the older name of the check before loading a lazy module (replaced by canMatch).
  7. Activation: components are created or reused, router-outlets display them, and titles are set. The router emits NavigationEnd, and Scroll events 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.