Consider an Angular resolver that fetches user data before activating a route. If the resolver returns an error or fails, what will happen to the route activation?
import { Injectable } from '@angular/core'; import { Resolve, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router'; import { Observable, throwError } from 'rxjs'; import { catchError } from 'rxjs/operators'; import { UserService } from './user.service'; @Injectable({ providedIn: 'root' }) export class UserResolver implements Resolve<any> { constructor(private userService: UserService) {} resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<any> { return this.userService.getUser(route.params['id']).pipe( catchError(error => throwError(() => new Error('Failed to load user'))) ); } }
Think about how Angular handles errors in resolvers and what happens to navigation when a resolver fails.
If a resolver returns an error, Angular cancels the navigation by default. The route will not activate, preventing the component from loading with incomplete data.
Which of the following Angular resolver implementations correctly pre-fetches data and returns an observable?
Remember that resolvers must implement the Resolve interface and return an observable or promise without subscribing inside.
Option A correctly implements Resolve<any>, injects HttpClient, and returns an observable without subscribing. Option A misses constructor injection. Option A returns a promise from fetch but does not implement Resolve interface. Option A subscribes inside resolver which is incorrect.
Given this route configuration and resolver, what will the component receive as data in ActivatedRoute?
const routes = [ { path: 'profile/:id', component: ProfileComponent, resolve: { userData: UserResolver } } ]; @Injectable({ providedIn: 'root' }) export class UserResolver implements Resolve<User> { constructor(private userService: UserService) {} resolve(route: ActivatedRouteSnapshot): Observable<User> { return this.userService.getUser(route.params['id']); } } // In ProfileComponent constructor: constructor(private route: ActivatedRoute) { this.route.data.subscribe(data => { console.log(data.userData); }); }
Resolvers provide data to the route's data observable after resolving.
The resolver fetches user data and Angular waits for it before activating the route. The resolved data is available in route.data under the key userData.
Examine the following resolver code. Why does it cause a runtime error when navigating to the route?
export class ProductResolver implements Resolve<Product> { constructor(private productService: ProductService) {} resolve(route: ActivatedRouteSnapshot): Product { this.productService.getProduct(route.params['id']).subscribe(product => { return product; }); } }
Resolvers must return an observable or promise, not void or subscription.
The resolver subscribes inside and does not return anything from resolve, so it returns void. Angular expects an observable or promise to wait for. This causes a runtime error.
You want to pre-fetch both user data and settings before activating a route. Which approach correctly uses multiple resolvers in Angular routing?
Think about how Angular expects the resolve property to be structured for multiple resolvers.
Angular routing expects resolve to be an object mapping keys to resolver classes. Each key's resolved data is available in route.data under that key.