Using Angular Resolver to Pre Load Data
Tags: javascript, resolver, webdev, angular
Published: 2021-09-26 15:19:26

---
title: Using Angular Resolver to Pre Load Data
published: true
date: 2021-09-03 15:30:55 UTC
tags: javascript,resolver,webdevelopment,angular
canonical_url: https://medium.com/@redin.gaetan/angular-for-every-one-resolver-c900c8f7672a
---

#### Angular for Everyone: Part 13

#### How to Preload Data for Routed Component in Angular

![](https://cdn-images-1.medium.com/max/1024/1*PbhH3t5w7b_iTkN0P4SPDA.jpeg)

Hey, you know that one goal of new web developments is to make smoother data loading and content rendering. That’s now easy, thanks to Observables. But in some cases, we need to load the data before reaching a route:

- Generic components which don’t care about the source of the data
- We don’t want to display a page’s block if the necessary data are not loaded
- Separation of concerns

That’s why we need to know that Angular Routing provides a resolve feature that allows pre-load data before the end of the navigation.

First, what does Angular provide us for this feature?

```
interface Resolve<T> {
  **resolve** (route: ActivatedRouteSnapshot, 
          state: RouterStateSnapshot)
  : Observable<T> | Promise<T> | T;
}
```

Yes, we need to create a class that implements this interface. <T> designed the expected returned type.

Second, let’s implement our own resolver:

```
// feature4.resolver.ts

Injectable({
    providedIn: 'root',
})
_export class Feature4_Resolver _implements Resolve_<_any_> {
  constructor(private _myService: MyService) {}

  _public_ resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<_any_> { 
    return this._myService.getMyEntity();
  }
}
```

Here, I use any as type, please don’t do this. Give a real type. We are using Typescript, and this is just a learning purpose.

Third, let’s use it:

```
_const_ routes: _Routes_ = [
  {
    path: ':code',
    component: Feature4Component,
    resolve: {
      **myData** : Feature4Resolver,
    },
    ...
  }
];
```

At last, let’s get the data into our component:

```
@Component({
  ...
})
_export class_ Feature4Component _implements OnInit_ {
  ...

  _get_ data(): Observable<_any_> {
    _return this_._activatedRoute.data.pipe(_map_((data: _Data_) => data. **myData** ));
  }

  _constructor_(_private_ _activatedRoute: ActivatedRoute) {}
  ...
}
```

Tip: note the location of your resolver in your routing module is important. If you want to access to _myData_ into _Feature4Component’s_ children, you will have to use the parent ActivatedRoute data: this.\_route.parent.data.

### Conclusion

As you can see, the component doesn’t care about the service, which is called the data. It just gets it from the routing data. So it allows to do great things and to more separate concerns. BUT you have to know that the navigation will be blocked until the resolver has returned anything. So it’s not advised to use this feature for long, complex operations. The usual, adapt the implementation of your solution from your need, not the other way around.

Thanks for reading.

You can find the source code here:

[GitHub - GaetanRdn/medium-angular: This project is a support for my medium articles.](https://github.com/GaetanRdn/medium-angular)

### Learn More

- [Basic Routing in Angular](https://dev.to/gaetanrdn/basic-routing-in-angular-46gc)
- [Angular: Lazy-loading & Nested RouterOutlet](https://dev.to/gaetanrdn/angular-lazy-loading-nested-routeroutlet-kc6-temp-slug-6255937)
- [How to Use Angular Route Guards](https://dev.to/gaetanrdn/routes-access-controlled-by-angular-guards-8i1)
- [Angular for everyone: All about it](https://link.medium.com/SXPQgRn7xjb)