【问题标题】:how to handle same value from multiple datastreams in Angular RxJs如何在 Angular RxJs 中处理来自多个数据流的相同值
【发布时间】:2021-03-09 14:20:34
【问题描述】:

我有两个不同的数据流,它们为相同的对象提供修改后的属性值。我想编写一个订阅,以便每当两个 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$ 和 updatedItem$ 可以在修改已选择的项目时返回具有不同属性值的相同对象。

这是我第一次在 RxJs 上工作,对这部分有点困惑。我在 RxJS 运算符列表(如 concat、merge、combine)中进行了搜索,但大多数工作示例都针对不同的数据结构。还有没有其他更好的方法来实现这一点?

【问题讨论】:

标签: javascript angular rxjs rxjs-observables rxjs-pipeable-operators


【解决方案1】:

您可以使用merge 创建一个从两个源流中发出值的可观察对象:

import { merge } from 'rxjs';

...

merge(selection$, updatedItem$)
  .subscribe(item => {
    this.displayLatest(item)
  })

【讨论】:

  • 谢谢@Steve Holgado。它对我有用。
猜你喜欢
  • 2021-07-29
  • 2014-04-30
  • 1970-01-01
  • 2021-05-26
  • 2018-01-07
  • 2019-03-13
  • 2015-08-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多