Sub topic 12
Reading resolved data in a component
This chapter assumes you know Angular components, signals-based input(), and the Routes array. It also assumes you have read the previous chapter, where productResolver was attached to a route. Here you will learn the two ways a component can read what a resolver returned.
Where resolved data lives
The router stores each resolver's result under the key you chose in the resolve map. With this route configuration, the result is stored under product:
{
path: 'products/:id',
resolve: { product: productResolver },
loadComponent: () => import('./product-detail.component').then(m => m.ProductDetailComponent),
}The data is available when the component is created. The router waits for the resolver to finish before it activates the route, so the component never sees a half-loaded state.
Option 1: read ActivatedRoute.data
ActivatedRoute has a data property. It is an Observable that emits an object holding all resolved values. It also has a snapshot.data property with the current values.
import { Component, inject } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { toSignal } from '@angular/core/rxjs-interop';
import { map } from 'rxjs';
import { Product } from './product.service';
@Component({
selector: 'app-product-detail',
standalone: true,
template: `<h1>{{ product().name }}</h1>`,
})
export class ProductDetailComponent {
private route = inject(ActivatedRoute);
product = toSignal(
this.route.data.pipe(map(data => data['product'] as Product)),
{ requireSync: true },
);
}Subscribe to data instead of reading snapshot.data when the router may reuse the component. For example, going from /products/1 to /products/2 keeps the same component instance. The resolver runs again, and data emits the new value. A snapshot read in the constructor would still show product 1.
Option 2: bind to component inputs
If you enable withComponentInputBinding(), the router sets component inputs from route data. The input name must match the key in the resolve map.
import { Component, input } from '@angular/core';
import { Product } from './product.service';
@Component({
selector: 'app-product-detail',
standalone: true,
template: `<h1>{{ product().name }}</h1>`,
})
export class ProductDetailComponent {
product = input.required<Product>();
}
// In app config: provideRouter(routes, withComponentInputBinding())This removes the ActivatedRoute dependency, and the component is easier to test because you can set the input directly. The same feature also binds path parameters and query parameters to inputs with matching names.
Choosing between them
| Approach | Setup needed | Reacts to reuse | Best for |
|---|---|---|---|
| `ActivatedRoute.data` | None | Yes, if you subscribe | Existing apps, no global config |
| Input binding | `withComponentInputBinding()` | Yes, input updates | New apps, simpler components |
Common mistakes
- The key in
resolveand the input name differ, so the input stays unset.input.requiredthen throws an error. - You read
snapshot.dataonce and miss later updates when the component is reused. - You use a key such as
titleand forget that it is also a reserved route property. Pick distinct names for resolved data.
Exercise
A user navigates from /products/1 to /products/2 and the component stays on screen showing product 1. The component reads this.route.snapshot.data['product'] in its constructor. What is the cause, and what are two fixes?
Show answer
The router reuses the component instance for the same route with a different parameter, so the constructor does not run again and the snapshot read is stale. Fix it by subscribing to route.data (for example with toSignal), or by enabling withComponentInputBinding() and using an input named product, which the router updates on each navigation.