In the previous article, we have seen how to create modern page loader using Router Events in Angular 6. It’s nice to show a page loader when navigating one page/component to another page/component, but there are two main issues here,
- The Component view is displayed before loading data is needed to display on that component view
- Page loader completes its processing before the component view is loaded
We will see in this article how we can resolve these issues.

While working on the Angular live project, we use a real-world API and there might be some delay before the data to display is returned from the server and in that case, we don’t want to display a blank component to the users when waiting for the data. Router Resolve API helps us a lot to resolve this issue.
It's preferable to pre-fetch data from the server so it's ready the moment the route is activated. This also allows you to handle errors before routing to the component.
Check the Angular official documentation here for more details.
In short, if you want to delay rendering the routed component until all necessary data to be displayed on this component is fetched from the server, Resolve API helps us.
Application after applying the Resolve will work like below,

To implement the Resolve API in an Angular application, we need to import Resolve interface from @angular/router package, implement Resolve interface and implement the Resolve() method. This method can return a Promise or an Observable.
Let’s understand this powerful API with the help of the below example.
We will build an Angular application where we will call JSONPlaceHolder API to get post data and will show the data in the user-posts component. Here, we will make sure that my user-posts component will not load or display until all user posts data from API is ready to load or display.
- export interface IPost {
- userId: string;
- id: number;
- title: string;
- body: string;
- }
Creating a service (post.service.ts)
- import { Injectable } from "@angular/core";
- import { HttpClient } from "@angular/common/http";
- import { Observable } from "rxjs";
- import { delay } from "rxjs/operators";
- import { IPost } from "../post.interface.ts";
- @Injectable({
- providedIn: "root"
- })
- export class PostService {
- private url = "http://jsonplaceholder.typicode.com/posts";
- constructor(private http: HttpClient) {}
- getPosts(): Observable<IPost[]> {
- return this.http.get<IPost[]>(this.url).pipe(delay(1000));
- }
- }
Creating a Resolver (user-posts.resolve.ts)
- import { Injectable } from "@angular/core";
- import { Resolve, ActivatedRouteSnapshot } from "@angular/router";
- import { Observable } from "rxjs";
- import { PostService } from "./../services/post.service";
- import { IPost } from "../post.interface.ts";
- @Injectable()
- export class UserPostsResolve implements Resolve<IPost[]> {
- constructor(private postService: PostService) {}
- resolve(route: ActivatedRouteSnapshot): Observable<IPost[]> {
- return this.postService.getPosts();
- }
- }

Darshan KhachanePosted Jun 17, 2019, 7:28 AM
What if I need to pass parameter to resolver? & I need to write multiple methods to get data on single component, is it possible
Debasish TutuPosted Nov 8, 2018, 12:58 AM
Informative Article