简体   繁体   English

异步管道的正确方法

[英]Proper approach for Async Pipe

I'm building an Angular app and trying to use the async pipe whenever possible to handle Observable suscriptions.我正在构建一个 Angular 应用程序,并尽可能使用async管道来处理 Observable 订阅。

I'm still to exactly sure of when and why I should use it, most of the time I've seen that if I don't need to make any changes to the coming data I can just use it and show the data as-is ;我仍然完全确定何时以及为什么应该使用它,大多数时候我已经看到,如果我不需要对即将到来的数据进行任何更改,我可以使用它并将数据显示为-是 but if I need to to something to any piece of the data beforehand I should manually subscribe in my typescript code and handle everything there before displaying it.但是如果我需要事先对任何数据进行处理,我应该手动订阅我的打字稿代码并在显示之前处理那里的所有内容。

So for example if I have an array of objects and I need to manipulate a string in one of the object's properties it would be better to manually subscribe, handling the response and then displaying that in my template.因此,例如,如果我有一个对象数组并且我需要在对象的一个​​属性中操作一个字符串,那么最好手动订阅、处理响应然后在我的模板中显示它。

Is this assumption correct?这个假设正确吗?

I have used both types of observables within components and these are my reasons (there are probably others that I am not aware of):我在组件中使用了两种类型的 observables,这些是我的原因(可能还有其他我不知道的):

Reasons for using a subscribed observable:使用订阅的 observable 的原因:

  1. To control subscribing and unsubscribing subscriptions manually.手动控制订阅和取消订阅。
  2. Synchronizing the loading and manipulation of data within a component before internal use.在内部使用之前同步组件内数据的加载和操作。
  3. When the subscribed data is used internally (non-visually) within the component, such as a service or computations.当订阅的数据在组件内部(非可视化)使用时,例如服务或计算。

Reasons for using an asynchronous observable pipe:使用异步可观察管道的原因:

  1. Subscribing and unsubscribing of subscriptions of observables is handled automatically.订阅和取消订阅 observable 的订阅是自动处理的。
  2. Synchronizing the loading and manipulation of data within a component before use within the HTML template.在 HTML 模板中使用之前同步组件内数据的加载和操作。
  3. When there are a number of HTML elements that depend on subscribed data and you would like the subscriptions released automatically after the component is destroyed.当有许多 HTML 元素依赖于订阅的数据,并且您希望在组件销毁后自动释放订阅。

In both cases you can load and manipulate subscribed data within your component before usage.在这两种情况下,您都可以在使用前加载和操作组件内的订阅数据。

An example of each is below:每个示例如下:

Subscription based基于订阅

TS TS

someData: SomeClass[] = [
  { id: 1, desc: 'One', data: 100 },
  { id: 2, desc: 'Two', data: 200 },
  { id: 3, desc: 'Three', data: 300 }
];
someData$: Observable<SomeClass[]>;

this.someData$ = of(this.someData).subscribe((res) => {
  this.someData = res.map((r) => {
    r.data = Math.floor(r.data * 1.1);
    return r;
  });
});

Asynchronous observable pipe异步可观察管道

TS TS

...
someData: SomeClass[] = [];
someData$: Subscription;

this.someData$ = of(this.someData).pipe(
  map((res) => {
    res.map((r) => {
      r.data = Math.floor(r.data * 1.1);
    });
    return res;
  })
);

HTML (for both options) HTML(对于两个选项)

<li *ngFor="let data of someData$ | async">
  Item={{ data.desc }}. Value={{ data.data }}
</li>

To summarize, the usage of either option depends on the complexity of your component, it's type (visual or non-visual) and how you would like to manage the memory management of subscriptions.总而言之,任一选项的使用取决于您的组件的复杂性、它的类型(可视或非可视)以及您希望如何管理订阅的内存管理。

The answer to the original question is no, it is not necessarily better to manually subscribe when calculations/pre-processing are involved.原始问题的答案是否定的,在涉及计算/预处理时手动订阅不一定更好。 You can also use an asynchronous pipe to do likewise as I showed with the two equivalent examples above.你也可以使用异步管道来做同样的事情,就像我在上面两个等效的例子中展示的那样。

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

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