简体   繁体   English

如何使用 rxjs 从另一个可观察对象创建可观察的 object?

[英]How to use rxjs to create an object observable from another observable?

I would like to expose an observable in my service, which emits a value each time a BehaviorSubject is assigned a value (and after filtering it from a list).我想在我的服务中公开一个 observable,每次为 BehaviorSubject 分配一个值(并在从列表中过滤它之后)时,它都会发出一个值。 Example implementation:示例实现:

export class MyService {

   // Given a list of all object
   private readonly allObjects$: Observable<SomeObject[]>;  
   // An id of a SomeObject instance
   private readonly mySubject = new BehaviorSubject<string|undefined>(undefined);

   // Expose a single instance from the list of all objects.
   public readonly myObject$: Observable<SomeObject|undefined>;   

   constructor() {

     
     this.myObject$ = 

        // Pipe in the object id (i.e.: 123)
        this.mySubject.pipe(

        // Add the list of all objects
        withLatestFrom(this.allObjects$),

        // Filter out the object whose id is 123 from the list of objects. This filtered 
        // object should be the value emitted by myObject$.
        switchMap(
            (info: [string, SomeObject[]]) =>
                info[1].filter(t => t.name === info[0])));
   }
}

Usage:用法:

  mySubject.next('123')  
  this.myObject$.subscribe(console.log)  // prints: SomeObject(with id 123)

However the above snippet produces this error (for the withLatestFrom operator):然而,上面的代码片段会产生这个错误(对于withLatestFrom运算符):

Argument of type 'OperatorFunction<string | undefined, [string | undefined, 
SomeObject[]]>' is not assignable to parameter of type 'OperatorFunction<string | 
undefined, [string, SomeObject[]]>'.

What am I doing wrong?我究竟做错了什么? How do I fix this?我该如何解决?

You have defined BehaviorSubject to hold either string or undefined value.您已将BehaviorSubject定义为保存stringundefined值。 So the info array defined within switchMap can have the 0th element as either string or undefined , and hence the type definition needs to specified accordingly.因此,在switchMap中定义的info数组的第 0 个元素可以是stringundefined ,因此需要相应地指定类型定义。 It should be:它应该是:

    info: [string | undefined, SomeObject[]]

You don't need a higher order observable to subscribe to another observable for this use case, instead of using switchMap(), why don't you use map() or filter()?对于这个用例,您不需要更高阶的 observable 来订阅另一个 observable,而不是使用 switchMap(),为什么不使用 map() 或 filter()?

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

相关问题 如何在 RxJS 中创建一个依赖于另一个 Observable 的 Observable - How to create an Observable that depends on another Observable in RxJS RxJS 等待 observable 然后创建另一个 observable,依此类推 - RxJS wait for observable then create another observable, and so on 如何使用 rxjs map 通过另一个不同类型的对象列表过滤出 object 的可观察列表 - How to use rxjs map to filter out an observable list of object by another list of objects of different type 如何取消订阅RxJS 5可观察? - How to unsubscribe from an RxJS 5 observable? RXJS:如何使用 takeUntil 直到另一个 Observable 完成(而不是发出) - RXJS: How to use takeUntil until another Observable completes (and not emits) 如何在 rxjs 中通过循环使用两个可观察函数一个在另一个内部? - How to use two observable functions one inside another with loop in rxjs? Rxjs:map 数组字段中的每个元素来自 object 的可观察对象和另一个可观察对象 - Rxjs: map each element in an array field from an observable of object with another observable rxjs:如何从 catchError 返回另一个可观察的结果 - rxjs: how to return the result from another observable from catchError 来自数组的 Observable 中的 RxJs Observable - RxJs Observable in Observable from Array 如何从另一个 observable 的结果创建新的 observable? - How to create new observable from the result of another observable?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM