5.8: Adding Route Resolvers
As our page loads and a component's ngOnInit()
method fires - triggering a call to our API - there may be a delay between the view rendering and the data initially appearing. The data will show up, but there may be errors in the console and the user experience isn't the best.
One solution is to use spinning loading icons.
We can also use route resolvers to load the data before the layout is rendered.
This article from alligator.io explains route resolvers much better than the official docs which don't offer much in the way of explanation.
Basically, think of them as being in the same 'family' as guards. They are run when a user navigates to a route - a guard checks whether a user is allowed to access to that route and a resolver will preemptively load the data.
Add MemberDetailResolver
Add a new folder in the /src/app
folder called resolvers
.
Inside of that folder, add a new file called member-detail.resolver.ts
Add the resolver to the providers array in app.module.ts:
Next, we need to add it to our routes inside of routes.ts
.
Using the Resolver in the Component
In member-detail.component.ts
, change ngOnInit()
to subscribe to data from the resolver:
We can also delete the loadUser()
method now.
Adding MemberListResolver
Let's go ahead and create one for the MemberListComponent
as well. It will almost all be the same code. You can copy the member-detail.resolver.ts
file and rename it to member-list.resolver.ts
. Make the following changes:
Add it to the providers array as well
And, also to the routes:
Update member-list.component.ts to use the resolver
Delete the loadUsers()
method and ensure that the data still loads.
Last updated