Sub topic 10
The order in which guards and resolvers run
This chapter assumes you know how to write a functional guard and a ResolveFn, and that you have a Routes array with nested and lazy-loaded routes. Here you will learn the sequence the router follows, so you can predict when each of your functions runs and avoid wasted work.
The sequence for one navigation
When a navigation starts, the router runs its checks in a fixed order:
canMatchguards run while the router matches the URL against the route configuration.canDeactivateguards run on the components being left.- Any lazy-loaded configuration is loaded.
canActivateChildguards run on parent routes, thencanActivateguards run on the routes being entered.- Resolvers run.
- The router activates the new components.
Resolvers run last on purpose. A guard that rejects the navigation prevents the data request from being sent at all.
Order within each step
Within one route, guards in an array run in the order you list them. Across routes, canActivate and canActivateChild run from the parent down to the child. canDeactivate runs from the child up to the parent.
The router runs the guards of a given kind in parallel when they are independent. In particular, canActivate guards on sibling levels of the route tree can start together. The first guard that returns false or a UrlTree cancels the navigation. The router does not wait for the others to finish. If order matters to you, for example "check login before checking the role", put both checks in one guard or give them one parent-child relationship.
import { Routes } from '@angular/router';
import { authGuard } from './auth.guard';
import { adminMatchGuard } from './admin-match.guard';
import { productResolver } from './product.resolver';
export const routes: Routes = [
{
path: 'admin',
canMatch: [adminMatchGuard], // 1. decides if this config matches at all
canActivate: [authGuard], // 2. runs only if it matched
resolve: { product: productResolver }, // 3. runs only if guards passed
loadComponent: () =>
import('./product-detail.component').then(m => m.ProductDetailComponent),
},
];If adminMatchGuard returns false, the router skips this route and tries the next one. Neither authGuard nor the resolver runs, and the lazy chunk is not downloaded.
Resolvers and the parent-child relationship
Resolvers for a route run after all guards have passed. By default, a child route's resolver does not run again when only a child parameter changes, unless runGuardsAndResolvers says so. The default is paramsChange, which re-runs when route parameters change.
{
path: 'products/:id',
runGuardsAndResolvers: 'paramsOrQueryParamsChange',
resolve: { product: productResolver },
loadComponent: () =>
import('./product-detail.component').then(m => m.ProductDetailComponent),
}Other values include 'always' and 'pathParamsChange'. Choose the narrowest option, because every re-run repeats your guards and your data request.
Common mistakes
- Putting an expensive check in a resolver that a guard could have refused earlier.
- Assuming two guards on one route run one after the other.
- Expecting
canMatchto run again after the route has matched. It runs only during matching.
Exercise
A route has canMatch, canActivate and a resolver. The user is not logged in, and canActivate returns a UrlTree to /login. Which of the three functions ran, and was the resolver's HTTP request sent?
Show answer
canMatch ran first and returned true, so the route matched. canActivate then ran and returned the UrlTree, which redirected the navigation. The resolver never ran, so its HTTP request was not sent.