Sub topic 12
Loading Data Before Navigation with Resolvers
This chapter assumes you already know functional guards, route parameters, inject(), and basic RxJS Observables. It uses the withComponentInputBinding() setup from earlier chapters.
A resolver is a function the router runs before it activates a route. The router waits for the resolver's result and then hands that data to the component. The component never renders in an empty "loading" state, because its data already exists when it is created.
Where Resolvers Fit in a Navigation
Resolvers run after guards pass and before the component is created. If a guard rejects the navigation, the resolvers never run.
Writing a Resolver
A resolver has the type ResolveFn<T>. It receives the ActivatedRouteSnapshot, so it can read parameters through route.paramMap. It can return a value, a Promise, or an Observable.
// product.service.ts
import { Injectable, inject } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';
export interface Product {
id: string;
name: string;
}
@Injectable({ providedIn: 'root' })
export class ProductService {
private http = inject(HttpClient);
getProduct(id: string): Observable<Product> {
return this.http.get<Product>(`/api/products/${id}`);
}
}// product.resolver.ts
import { inject } from '@angular/core';
import { ResolveFn } from '@angular/router';
import { Observable } from 'rxjs';
import { Product, ProductService } from './product.service';
export const productResolver: ResolveFn<Product> = (route): Observable<Product> => {
const id = route.paramMap.get('id')!;
return inject(ProductService).getProduct(id);
};You register it under a key in the route's resolve object:
{ path: 'products/:id', component: ProductDetailComponent, resolve: { product: productResolver } }Reading Resolved Data
The router stores the result under the key you chose (product) in the route data. With withComponentInputBinding(), an input with the same name receives it directly:
import { Component, Input } from '@angular/core';
import { Product } from './product.service';
@Component({
selector: 'app-product-detail',
standalone: true,
template: `<h2>{{ product.name }}</h2>`
})
export class ProductDetailComponent {
@Input() product!: Product;
}If you don't use input binding, inject ActivatedRoute and read route.data, which is an Observable that emits again when the resolved data changes.
Common Errors
| Symptom | Cause | Fix |
|---|---|---|
| Navigation silently does nothing | The resolver's Observable completed without emitting | Make sure it emits at least once. An empty Observable cancels the navigation. |
| Navigation fails with an error | The HTTP request errored inside the resolver | Catch the error and return a fallback or a redirect |
| Data is stale after changing `?sort=` | Resolvers re-run only on path parameter changes by default | Set `runGuardsAndResolvers: 'paramsOrQueryParamsChange'` on the route |
| Only the first value arrives | The router takes the first emitted value only | Use a service or `route.data` for streaming updates |
Here is error handling that sends the user to the not-found page:
import { inject } from '@angular/core';
import { RedirectCommand, ResolveFn, Router } from '@angular/router';
import { catchError, of } from 'rxjs';
import { Product, ProductService } from './product.service';
export const safeProductResolver: ResolveFn<Product> = (route) => {
const router = inject(Router);
return inject(ProductService).getProduct(route.paramMap.get('id')!).pipe(
catchError(() => of(new RedirectCommand(router.parseUrl('/not-found'))))
);
};Exercise
A route products/:id uses productResolver. The user moves from /products/1?tab=specs to /products/1?tab=reviews. Does the resolver run again, and why?
Show answer
No. With the default runGuardsAndResolvers value of 'paramsChange', resolvers re-run only when path or matrix parameters change. Only a query parameter changed here. Setting runGuardsAndResolvers: 'paramsOrQueryParamsChange' on the route would make it re-run.
Exercise
Your resolver returns EMPTY when a product is missing. What does the user see?
Show answer
Nothing changes. The navigation is cancelled because the resolver completed without emitting, so the user stays on the current page. Return a RedirectCommand or a fallback value to handle the missing product explicitly.