Sub topic 2
What guards and resolvers are for
This chapter assumes you know TypeScript, Angular components and services, basic routing (the Routes array, router-outlet, route parameters), and how dependency injection works. It explains why guards and resolvers exist and what job each one does. Later chapters cover how to write them.
The problem they solve
A route configuration maps a URL to a component. By default, the router follows that mapping without asking questions. If the URL matches, the component loads and renders.
Real applications need more control than that:
- Some pages should only be visible to logged-in users.
- Some pages need data before they can render anything useful.
- Leaving a form with unsaved edits should ask for confirmation.
- Some routes should only exist for certain users, such as administrators.
Putting these checks inside components works, but it scatters the same logic across many files and lets the component render briefly before the check runs. The router provides two hooks that run earlier: guards and resolvers.
Guards decide whether navigation may proceed
A guard is a function the router calls during navigation. It answers one question: may this navigation continue?
A guard can allow the navigation, cancel it, or redirect to a different URL. Angular provides four guard types, each attached to a route property of the same name:
| Guard | Question it answers |
|---|---|
| `canMatch` | Should this route configuration be considered at all? |
| `canActivate` | May the user enter this route? |
| `canActivateChild` | May the user enter any child of this route? |
| `canDeactivate` | May the user leave this route? |
Guards are plain functions that can call inject() to reach services. You will write them in the next chapters.
Resolvers supply data before activation
A resolver is also a function the router calls during navigation, but its job is to fetch data. The router waits for the resolver to finish, then activates the route and makes the result available to the component.
This changes what the user sees. Without a resolver, the component renders first, then loads its data, so you must handle an empty state. With a resolver, the route does not appear until its data is ready.
Guards versus resolvers
| Row | Guard | Resolver |
|---|---|---|
| Purpose | Permit, block, or redirect | Load data |
| Result | Boolean or redirect | A value for the component |
| Typical example | Is the user logged in? | Fetch the product for `:id` |
Both can be asynchronous. Both run before the new component is created, so the user never sees a half-built page.
A first look
This route uses both. The guard runs first, then the resolver, and only then does ProductDetailComponent load.
import { Routes } from '@angular/router';
import { authGuard } from './auth.guard';
import { productResolver } from './product.resolver';
export const routes: Routes = [
{ path: 'login', loadComponent: () => import('./login.component').then(m => m.LoginComponent) },
{
path: 'products/:id',
canActivate: [authGuard],
resolve: { product: productResolver },
loadComponent: () => import('./product-detail.component').then(m => m.ProductDetailComponent),
},
];The key product in the resolve map becomes the name of the resolved data.
Exercise
A product page shows a blank layout for a moment, then fills in after an HTTP request finishes. Which router feature would make the page appear only once the data is ready, and which would stop anonymous users from reaching it?
Show answer
A resolver loads the product before the route activates, so the page appears with its data. A canActivate guard stops anonymous users, by returning false or redirecting them to the login page.