简体   繁体   English

角度路由解析器或onInit?

[英]angular route resolver or onInit?

What better for using resolver service or get data in OnInit hook?使用resolver服务或在 OnInit 钩子中获取数据有什么更好的? For example, if I need get data from 3 different sources into 1 page better user resolver or write code into ngOnInit?例如,如果我需要从 3 个不同来源获取数据到 1 页更好的用户解析器或将代码写入 ngOnInit?

code代码

ngOnInit() {
  service1.getData().subscribe(c => {
    this.data1 = c;
  });
  service2.getData().subscribe(c => {
    this.data2 = c;
  });
  service3.getData().subscribe(c => {
    this.data3 = c;
  });
}

OR

RouterModule.forRoot([{
  path: 'page/:id',
  component: blabla,
  resolve: {
    data1: service1,
    data2: service2,
    data3: service3
  }
}])

ngOnInit() {
  this.data1 = this.activatedRoute.snapshot.data.data1;
  this.data2 = this.activatedRoute.snapshot.data.data2;
  this.data3 = this.activatedRoute.snapshot.data.data3;
}

The main difference between resolvers and onInit is the synchronicity. resolversonInit之间的主要区别是同步性。

  • Resolver is synchronous. Resolver是同步的。

    • You should use it when you need the data before the component is loaded.在加载组件之前需要数据时应该使用它。
    • You block the component loading.您阻止了组件加载。
    • You don't inject the service into the component (you can't use other methods there)您没有将服务注入到组件中(您不能在那里使用其他方法)
  • OnInit is asynchronous (in your code). OnInit是异步的(在您的代码中)。

    • You should use it when there is no need for the data being available before loading the component.当在加载组件之前不需要数据可用时,您应该使用它。
    • You don't block the component loading.您不会阻止组件加载。
    • You inject the service into the component, therefore you can use other methods from this service.您将服务注入到组件中,因此您可以使用此服务的其他方法。

Take a look at this site: https://blog.thoughtram.io/angular/2016/10/10/resolving-route-data-in-angular-2.html看看这个网站: https : //blog.thoughtram.io/angular/2016/10/10/resolving-route-data-in-angular-2.html

Well it depends.嗯,这取决于。 If the component cannot function without the data1 , data2 , and data3 , then the resolve approach makes a lot of sense.如果组件在没有data1data2data3情况下无法运行,那么解析方法就很有意义。 An example would be you bank account page.一个例子是你的银行账户页面。 You do not really care about the page until you can see the details of your account.在您可以看到您帐户的详细信息之前,您并不真正关心该页面。

If you can show something to the user while he awaits for the data it might be better to get the data in the ngOnInit .如果您可以在用户等待数据时向他显示某些内容,最好在ngOnInit获取数据。 An example would be a product page, where data{1,2,3} is a stream of recommended products.一个例子是产品页面,其中data{1,2,3}是推荐产品的流。 The product page can still be shown, even without the recommendations, and the recommended products can continue to load while the user can interact with the rest of the page.即使没有推荐,产品页面仍然可以显示,并且推荐的产品可以继续加载,而用户可以与页面的其余部分进行交互。

It will be very hard for anyone to answer this question without a deeper understanding of your business logic requirements.如果不深入了解您的业务逻辑需求,任何人都很难回答这个问题。

It's important to understand that route resolvers and component ngOnInit are two different solutions to two different problems, meaning that one tool is not better than the other.重要的是要了解route resolverscomponent ngOnInit是针对两个不同问题的两种不同解决方案,这意味着一个工具并不比另一个更好。

I would say the best practice is to use both in your application, depending on the situation - sometimes it makes sense, UX-wise, to display a view synchronously (blocking until data is loaded) and other times it's better to display the view immediately and load data asynchronously.我会说最好的做法是在您的应用程序中使用两者,具体取决于情况 - 有时在用户体验方面同步显示视图(在加载数据之前阻塞)是有意义的,而其他时候最好立即显示视图并异步加载数据。

So, it's not an application design or architectural decision - It's a UX/UI decision.因此,这不是应用程序设计或架构决策——而是 UX/UI 决策。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM