Skip to content

Route Resolvers

A resolver fetches data before a route activates, so the component renders with its data already in place — no empty state, no flash of a loading skeleton.

That benefit comes with a cost that decides most design questions on this page: navigation is blocked while resolvers run, and the previous page stays on screen with no built-in feedback. A resolver is the right tool when the route is meaningless without the data. It is the wrong tool for anything the page could render around.

For where resolvers sit in the navigation pipeline, see Angular Routing in Depth.

export const orderResolver: ResolveFn<Order> = (route, state) =>
inject(OrderApiClient).getOrder(route.paramMap.get('id')!);
{
path: 'orders/:id',
component: OrderDetail,
resolve: { order: orderResolver },
}

ResolveFn<T> runs in an injection context, so inject() works. It receives the ActivatedRouteSnapshot and the RouterStateSnapshot, and may return T, Promise<T> or Observable<T>. As with guards, the router takes the first emitted value and unsubscribes.

With withComponentInputBinding() enabled, each resolve key becomes a component input:

provideRouter(routes, withComponentInputBinding());
@Component({ /* ... */ })
export class OrderDetail {
readonly order = input.required<Order>();
readonly lineCount = computed(() => this.order().lines.length);
}

Typed, no subscription, and testable by setting the input directly — no router involved.

private readonly route = inject(ActivatedRoute);
readonly order = toSignal(this.route.data.pipe(map((d) => d['order'] as Order)));

Note the cast. route.data is an untyped bag; the boundary is where type safety is lost, so cast once, in one place, to a declared interface.

Do not read resolved data from route.snapshot.data in a component that can be reused across param changes — the same staleness trap as route params.

Resolvers run parent first, and a child resolver can read the parent’s resolved data:

provideRouter([
{
path: 'users/:id',
resolve: { user: userResolver },
children: [
{
path: 'posts',
component: UserPosts,
resolve: {
posts: (route: ActivatedRouteSnapshot) => {
const user = route.parent?.data['user'] as User;
return inject(PostService).getPostsByUser(user.id);
},
},
},
],
},
]);

This is the sanctioned way to express a dependent load. Note that resolvers within a single route run in parallel, so two resolvers on the same route cannot depend on each other — the dependency has to be expressed through the parent/child relationship, or handled inside one resolver.

Three levels, and choosing deliberately matters because the default — an unhandled error cancels the navigation — looks to the user like a link that does nothing.

In the resolver. Most local, and lets navigation continue with a fallback:

export const orderResolver: ResolveFn<Order | null> = (route) =>
inject(OrderApiClient)
.getOrder(route.paramMap.get('id')!)
.pipe(catchError(() => of(null)));

The component then handles the null case. Use this when a missing record should still show a page.

Application-wide, at bootstrap. One policy for every navigation failure:

provideRouter(
routes,
withNavigationErrorHandler((error) => {
inject(ErrorReporter).report(error);
return inject(Router).createUrlTree(['/error']);
}),
);

Returning a UrlTree from the handler redirects; returning nothing leaves the navigation cancelled.

By subscribing to router events. For telemetry rather than control flow:

this.router.events
.pipe(filter((e): e is NavigationError => e instanceof NavigationError), takeUntilDestroyed())
.subscribe((e) => this.reporter.report(e.error, { url: e.url }));

A common combination: let the resolver deliberately fail for a genuine 404 (so the route never activates and the user lands on a not-found page), and use withNavigationErrorHandler for everything unexpected.

Because resolvers block, the application needs a visible indicator or it appears frozen:

readonly navigating = toSignal(
this.router.events.pipe(
filter((e) =>
e instanceof NavigationStart ||
e instanceof NavigationEnd ||
e instanceof NavigationCancel ||
e instanceof NavigationError,
),
map((e) => e instanceof NavigationStart),
),
{ initialValue: false },
);

Handle all four completion events, or the indicator can get stuck on after a cancelled navigation.

Angular’s own guidance for resolvers, with the reasoning:

  • Keep them lightweight. Fetch what the route needs to be valid, not everything the page will eventually show. Every extra field is added latency on a blocked navigation.
  • Handle errors. Always. An unhandled error is a dead link.
  • Cache. Re-resolving unchanged reference data on every visit is pure latency for no information.
  • Give feedback. Navigation is blocked; the user needs to know.
  • Set timeouts. A resolver that hangs blocks navigation indefinitely. timeout() from RxJS plus a catchError bounds the damage.
  • Type the results. route.data['x'] is any; give it an interface at the boundary.

Increasingly, the answer is “load in the component instead”:

@Component({
template: `
@if (order.isLoading()) {
<order-skeleton />
} @else if (order.error()) {
<error-panel [error]="order.error()" (retry)="order.reload()" />
} @else if (order.hasValue()) {
<order-view [order]="order.value()" />
}
`,
})
export class OrderDetail {
readonly id = input.required<string>();
readonly order = httpResource<Order>(() => `/api/orders/${this.id()}`);
}

This navigates instantly, renders the shell immediately, and gives the user a real loading state, an error state and a retry — all things a resolver has to bolt on from outside.

Keep the resolver when:

  • The route must genuinely fail for missing data (a 404 that should not activate the route).
  • A guard’s decision depends on the fetched data.
  • SEO or SSR requires the data present in the first rendered HTML.
  • The page is meaningless in a partial state, and a skeleton would be worse than a short block.

Otherwise, prefer in-component loading. The frozen-page experience is worse than a skeleton in almost every case where the data is not load-bearing for the route itself.

Like guards, resolvers are plain functions:

describe('orderResolver', () => {
it('fetches the order named by the route param', async () => {
const api = { getOrder: (id: string) => of({ id, total: 42 } as Order) };
TestBed.configureTestingModule({
providers: [{ provide: OrderApiClient, useValue: api }],
});
const snapshot = { paramMap: convertToParamMap({ id: '7' }) } as ActivatedRouteSnapshot;
const result = await firstValueFrom(
TestBed.runInInjectionContext(
() => orderResolver(snapshot, {} as RouterStateSnapshot),
) as Observable<Order>,
);
expect(result.id).toBe('7');
});
});

convertToParamMap from @angular/router builds a real ParamMap from a plain object, which is cleaner than casting.

Mistake Consequence Fix
Fetching everything the page might need Long blocked navigation Resolve only what the route requires
No error handling Navigation cancels silently; link looks dead catchError, or withNavigationErrorHandler
No loading indicator Application appears frozen Drive one from router events
Reading snapshot.data in a reusable component Stale data on param change Component input binding, or subscribe to route.data
Two resolvers on one route depending on each other They run in parallel; one sees undefined Use parent/child, or combine into one resolver
Untyped route.data['x'] spread across the codebase any everywhere Cast once at the boundary to a declared interface
Resolver where a skeleton would do Frozen page instead of progressive render Load in the component with resource / httpResource
No timeout Navigation blocked indefinitely timeout() + catchError