[英]how to handle same value from multiple datastreams in Angular RxJs
I have two different data streams which gives same object with modified property values.我有两个不同的数据流,它们给出了相同的 object 并修改了属性值。 I want to write a single subscription so that whenever any of the two DataStream notifies about property modification I can reuse the same code.
我想编写一个订阅,以便每当两个 DataStream 中的任何一个通知属性修改时,我都可以重用相同的代码。
const selectedItems$ = this.grid.onSelect.pipe(.... this gives me the selected object);
const updatedItem$ = this.fetchData.onUpdate.pipe(.....this gives me the updated object);
displayItem$(selection$, updatedItem$) {
(..here i want to get notified whenever there is change in).subscribe(item => {
this.displayLatest(item)
})
}
selectedItem$ and updatedItem$ can return same object with different property values when the already selected item is modified. selectedItem$ 和 updatedItem$ 可以返回相同的 object 在修改已选择的项目时具有不同的属性值。
This is the first time I am working on RxJs and bit confused about this part.这是我第一次在 RxJs 上工作,对这部分有点困惑。 I searched in RxJS operators list (like concat, merge, combine) but most of the working examples are for different data structures.
我在 RxJS 运算符列表(如 concat、merge、combine)中进行了搜索,但大多数工作示例针对不同的数据结构。 Also is there any other better way to achieve this?
还有其他更好的方法来实现这一点吗?
You can use merge
to create an observable that emits values from both source streams:您可以使用
merge
创建一个从两个源流中发出值的可观察对象:
import { merge } from 'rxjs';
...
merge(selection$, updatedItem$)
.subscribe(item => {
this.displayLatest(item)
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.