【问题标题】:RxJS convert Observable<string[]> into Observable<string>RxJS 将 Observable<string[]> 转换为 Observable<string>
【发布时间】:2023-03-23 00:16:01
【问题描述】:

RxJS 5.5.6。我正在尝试将Observable&lt;string[]&gt; 转换为Observable&lt;string&gt;

我不确定mergeAll 运算符是否是我正在寻找的

const o1: Observable<string[]> = of(['a', 'b', 'c']);
const o2: Observable<string> = o1.pipe(
  mergeAll()
);

Typescript 将返回此错误:

Type 'Observable<string | string[]>' is not assignable to type 'Observable<string>'.
  Type 'string | string[]' is not assignable to type 'string'.
    Type 'string[]' is not assignable to type 'string'.

我接收 Observable 作为参数,我可以改变构造方式。

【问题讨论】:

    标签: typescript rxjs rxjs5


    【解决方案1】:

    您似乎遇到了已报告的 RxJS 问题,请参阅 https://github.com/ReactiveX/rxjs/issues/2759 和最近的 https://github.com/ReactiveX/rxjs/issues/3290

    从 cmets 看来,它似乎要到 RxJS 6 才能修复。

    但是,您始终可以使用mergeMap(o =&gt; o)/concatMap(o =&gt; o) 而不是mergeAll()/concatAll()

    例如:

    const o1: Observable<string[]> = of(['a', 'b', 'c']);
    const o2: Observable<string> = o1.pipe(
      mergeMap(o => o)
    );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-28
      • 1970-01-01
      • 1970-01-01
      • 2019-07-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-24
      相关资源
      最近更新 更多