简体   繁体   English

* ngFor与异步管道和热套接字连接的循环可观察到?

[英]*ngFor loop with async pipe and hot socket connected observable?

Suppose we have alerts rendering into a list using *ngFor like this: 假设我们使用*ngFor将警报呈现到列表中,如下所示:

*ngFor="let alert of alerts | async"

Is it possible to have the alerts property be a hot observable that is being pushed to by the server and causing the list containing the alerts to refresh correspondingly? 服务器是否可以将alerts属性设置为可观察到的热点,并导致包含警报的列表相应刷新? If so what type of Observable(s) would be appropriate in this type of context? 如果是这样,则在这种类型的上下文中哪种类型的Observable适合?

The async pipe subscribes to the Observable for you and subsequently cleans up the Subscription when it is no longer needed. async管道为您订阅了Observable,随后在不再需要订阅时清理了Subscription。 You can apply the async pipe to any Observable, hot or cold, and it will create a new Subscription to render the content of that Observable to the template (or pass it to a child component through an input). 您可以将async管道应用于任何可观察的对象,无论是热对象还是冷对象,它将创建一个新的订阅,以将该对象的内容呈现给模板(或通过输入将其传递给子组件)。 For example, let's say I have an alerts$ Observable: 例如,假设我有一个alerts$ Observable:

alerts$: Observable<Alert[]> = alertService.getAlerts();

And I want to render a list of those alerts, like you show in your question: 我想呈现这些警报的列表,就像您在问题中显示的那样:

<ul>
  <li *ngFor="let alert of alerts$ | async">{{ alert }}</li>
</ul>

I can also subscribe to the alerts$ Observable in my template and do stuff with the data there. 我还可以在模板中订阅Alerts $ Observable并处理其中的数据。

Let's make another property to hold the alert and a property to hold the subscription for cleanup. 让我们创建另一个属性来保存警报,并创建一个属性来保存订阅以进行清理。

mostRecentAlert: Alert;
alertsSubscription: Subscription;

And give it a value in the ngOnInit lifecycle hook. 并在ngOnInit生命周期挂钩中ngOnInit赋予一个值。

ngOnInit() {
  this.alertsSubScription = alerts$.subscribe((next: Alert[]) => {
    mostRecentAlert = next[0];
  }
}

Then clean it up in the ngOnDestroy lifecycle hook. 然后在ngOnDestroy生命周期挂钩中对其进行ngOnDestroy

ngOnDestroy() {
 this.alertsSubscription.unsubscribe();
}

alerts$ is now technically a "hot" Observable because it has an active subscription, but you can still use it in your template as before. alerts$现在在技术上是“热门”可观察的,因为它具有有效的订阅,但是您仍然可以像以前一样在模板中使用它。

In short, the async pipe is just a handy tool for subscribing to Observables in the template. 简而言之, async管道只是用于在模板中预订Observable的便捷工具。 It allows you to render asynchronous data without handling any of the clean up. 它使您无需处理任何清理就可以呈现异步数据。

To answer your question about what type of Observables would be appropriate for this context: whenever you are getting data through some sort of service or store (if you're using ngrx or something similar), it will most likely arrive as a stream. 要回答有关哪种类型的Observable适用于这种情况的问题:每当您通过某种服务或存储(如果您使用的是ngrx或类似的东西)获取数据时,它很可能以流的形式到达。 Even Angular's Http utilities use rxjs Observables. 甚至Angular的Http实用程序也使用rxjs Observables。 This allows you to use all of the powerful rxjs operators to mold your data into the form you want and just leave that final Observable "cold" in your component class. 这使您可以使用所有强大的rxjs运算符将数据模制成所需的形式,并将最终的Observable“ cold”保留在组件类中。 Then just throw the async pipe on it in your template and you have a very neat way to render asynchronous data or pass it into other components. 然后,将async管道放在模板中即可,您可以通过一种非常巧妙的方式来呈现异步数据或将其传递给其他组件。

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

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