简体   繁体   English

如何在 Angular RxJs 中处理来自多个数据流的相同值

[英]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.

相关问题 Angular Rxjs:如何处理影响基本 observable stream 的多个事件 - Angular Rxjs: How to handle multiple events that affects a base observable stream 如何使用Angular处理RxJS中的错误 - How to handle errors in RxJS using Angular 如何从rxjs流中过滤单个值以在angular 2组件中打印 - How to filter single value from an rxjs stream to print in angular 2 component Angular / rxJS:combineLatest - 如何处理 404 错误 - Angular / rxJS: combineLatest - how to handle 404 Errors Angular HttpInterceptor:如何在多种条件下使用RxJS - Angular HttpInterceptor: How to use RxJS for multiple conditions 如何使用 rxjs / angular observable 发布值 - how to use rxjs / angular observable to publish a value 如何使用 RxJS 处理 401 Unauthorized 响应与 Angular 8 服务中的其他错误不同? - How can I handle a 401 Unauthorized response differently from other errors in an Angular 8 service using RxJS? 来自多个函数的 Angular/RxJS 可观察管道 - Angular/RxJS Observable piping from multiple functions Angular RxJs:如何根据触发顺序处理输出事件优先级 - Angular RxJs : how to handle Outputs events priority based on firing order RxJS和Angular HttpClient:如何异步转换值? - RxJS & Angular HttpClient: How to transform value asynchronously?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM