DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Coding
  3. Frameworks
  4. Angular Resolvers

Angular Resolvers

Get started with Angular Resolvers fast!

Astha Gupta user avatar by
Astha Gupta
·
Oct. 16, 19 · Tutorial
Like (10)
Save
Tweet
Share
15.99K Views

Join the DZone community and get the full member experience.

Join For Free

corner-of-soccer-field

What Is a Resolver?

To ensure, certain data is loaded from an API response before the route is actually activated, we use Route Resolvers.

In other words, to prefetch the data for a particular route before the component is loaded.

Why Use a Resolver?

Assume, there is a case when, in an Angular application, in a component, some elements are displayed spontaneously, but other components are loaded after Observable is subscribed successfully which might take some time to render on UI. Here, Resolvers come to the rescue. They allow applications to retrieve data first from an API response before the component gets loaded, allowing for route navigation.

Let’s, start implementing Resolvers.

You may also like: Angular Observables and Promises – How to Use Them. 

Creating a Resolver Service

  • Importing Resolve interface from @angular/router, which provides a resolve method that ensures anything's data that is returned is resolved.
  •  Resolve() has two parameters — route which is ActivatedRouteSnapshot and state which is RouterStateSnapshot as below:

Resolve() interface

Resolve() interface
  • The Resolve() method resolves an observable:

 employee.resolver.ts

employee.resolver.ts

Adding a Resolver in Route

We are adding the resolver, employee.resolve.ts, in our routes, and the resolve method, which was added in our resolver will return the resolved data into the key named empData.

 app.module.ts

app.module.ts

Accessing Resolved Data in our EmployeeDetailsComponent 

Resolved data can be accessed using the key defined in our routes using the ActivatedRoute’s snapshot object.

employee-details.component.ts

employee-details.component.ts

Displaying the Resolved Data in HTML

employee-details.component.html

employee-details.component.html

And that's how the resolver works — make your data available before the router starts working and components get loaded!

Happy Reading!


Further Reading

  • Angular 8: All You Need to Know.
  • Full-Stack App With Angular 8 and Web API [Video]: Part 1.
  • Login With Facebook and Google Using Angular 8.
AngularJS

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Browser Engines: The Crux of Cross-Browser Compatibility
  • Spring Boot vs Eclipse MicroProfile: Resident Set Size (RSS) and Time to First Request (TFR) Comparative
  • Key Elements of Site Reliability Engineering (SRE)
  • ClickHouse: A Blazingly Fast DBMS With Full SQL Join Support

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: