Why would you use a resolver with Angular

后端 未结 5 858
独厮守ぢ
独厮守ぢ 2020-12-12 20:58

I like the idea of resolvers.

You can say that:

  • for a given route you expect some data to be loaded first
  • you can just have a reall
5条回答
  •  攒了一身酷
    2020-12-12 21:26

    This is why I would use a resolver:

    • Single Responsibility, my component needs some data, in some cases this data are provided by a cache or state, when missing I need first to fetch them, and pass directly, without changing my component. Example: A Search Result page with a list myapp.com/lists, navigate to one element of the list myapp.com/lists/1, showing the details of that element, no need to fetch the data, already done by the search. Then let suppose you navigate directly to myapp.com/lists/1 you need to fetch and then navigate to the component
    • Isolate your component from router logic
    • My component should not manage the loading state of a fetch request, it's only responsibility is showing the data

    Think to the resolver as a middleware between your app and your component, you can manage the loading view in the parent component, where is included.

提交回复
热议问题