Y
Y
Yura Yurievich2017-03-02 19:52:37
JavaScript
Yura Yurievich, 2017-03-02 19:52:37

What are resolvers used for in Angular 2?

I was at an interview, this is what my application structure looked like:


| app-
| user.component.ts-
| users-list.component.ts
-| posts.component.ts-
| data.service.ts-
| user.resolver.ts
//The project is much larger, but this abstract structure will suffice to understand my problem.

When I click on a user, in the users.list component , it goes to the ''/user/:id' route. The user component is set for it , in the body of its constructor, I get a list of users and their ports, something like this:
constructor (data: data.service ... ) {
    data.subscribe(... this.user = user);
    data.subscribe(... this.postsOfUser = userPosts);
    //Код похож на оригинал, но короче.
}

So, at the interview they told me that this is a bad approach, it’s better to use resolvers, I googled, looked and realized that they are needed, in my opinion, only as a layer between the transition and the component, and just the data selection that I do in the constructor of the user component , I had to do in the resolver in order to simplify the constructor.
As a result, they do not give anything, but only reduce the component code by transferring it to the resolver?
That's how I understood why they are needed, please point out if I'm wrong or right, thanks!
Here is what my user.resolver.ts does
resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<User> {
        return this.rest.getUser(route.params['id']);
    }

Next comes the loading of the component ( user.component.ts ) during the route:
export class UserPostsComponent {
    user: User;

    constructor(private router: ActivatedRoute, private rest: RestService) {
        this.user = router.snapshot.data['user'];
        
        rest.getAllPosts(router.snapshot.params['id']);
    }
}

That is, I only get the user through the resolver, and that's it, but the posts are in the user component. If I also want to receive posts in the resolver, then I need a separate file for it ...
Please, help me understand, am I thinking right?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
N
Nicholas, 2017-03-02
@healqq

Generally speaking, you need to be careful with resolvers.
Here we first shoved everything into resolvers, and then we removed some of the non-critical content back from them, because page display was delayed for a very long time. In resolvers, you should leave only that without which you cannot show the view. Also, resolvers can be used to check access or some other things. After all, if reject gets into the resolver, the transition will not occur.

D
DarthJS, 2017-03-03
@DarthJS

Resolve is used in cases where you need to wait for data that is transmitted to you with a delay (asynchronous). For example, in Angular 1, when a promise comes to you, you need to resolve it (translating the word/parameter often helps, njulf ​​makes it clearer what it does), and then inject it into the controller. That is, the view waits for the data to be prepared in the route, and only then displays it.
The situation is the same in Angular 2, only Observables have appeared, in addition to Promise.

M
Maxim Pak, 2018-11-20
@PML

As written above. Resolve allows you to get data before the component is loaded by the route. Because Receiving data from the server can be long and so that the user does not look at the white screen, resolve is used, which receives data until the moment when routing worked for you.
It is well described here why and for what: https://bxnotes.ru/conspect/angular-5-the-complete...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question