A resolve guard is a type of a router guard, as mentioned in Chapter 14, Design Authentication and Authorization. A resolve guard can load necessary data for a component by reading record IDs from route parameters, asynchronously load the data and have it ready by the time the component activates and initializes.
The major advantages for a resolve guard includes reusability of loading logic, reduction of boilerplate code, and also shedding dependencies, because the component can receive the data it needs without having to import any service:
- Create a new
user.resolve.ts
class underuser/user
:
src/app/user/user/user.resolve.ts
import { Injectable } from '@angular/core'
import { Resolve, ActivatedRouteSnapshot } from '@angular/router'
import { UserService } from './user.service'
import { IUser } from './user'
@Injectable()
export class UserResolve implements Resolve<IUser> {
constructor(private userService: UserService) {}
resolve(route: ActivatedRouteSnapshot) {
...