Functional resolvers replace the deprecated class-based Resolve interface, letting you use inject() to pull dependencies directly inside a plain function passed to the route config. This example guards against malformed route parameters before hitting the service layer, ensuring the component only ever receives a validated Product or null rather than performing ad-hoc parsing in ngOnInit. Because the resolver runs before activation, the component template can assume the data is already present via the route's resolved data, eliminating loading-state flicker for this specific navigation.
import { ResolveFn, ActivatedRouteSnapshot } from '@angular/router';import { inject } from '@angular/core';import { ProductService } from './product.service';import { Product } from './product.model';export const productResolver: ResolveFn<Product | null> = (route: ActivatedRouteSnapshot) => {const service = inject(ProductService);const id = route.paramMap.get('id');if (!id || !/^[0-9]+$/.test(id)) {return null;}return service.getById(Number(id));};// Route config:// { path: 'product/:id', component: ProductDetail, resolve: { product: productResolver } }