Rxjs 连接示例

Elg*_*des 1 rxjs angular

使用rxjs有一段时间了,但有些东西不明白,其中之一就是concat。rxjs 网站的示例:

    const sourceOne = of(1, 2, 3);
    //emits 4,5,6
    const sourceTwo = of(4, 5, 6);
    //emit values from sourceOne, when complete, subscribe to sourceTwo
    const example = sourceOne.pipe(concat(sourceTwo));
Run Code Online (Sandbox Code Playgroud)

但是,在我的角度项目中,这给了我这个错误:

TS2345: Argument of type 'Observable<number>' is not assignable to parameter of type 'OperatorFunction<number, {}>'.
      Type 'Observable<number>' provides no match for the signature '(source: Observable<number>): Observable<{}>'.
Run Code Online (Sandbox Code Playgroud)

不知道为什么,有人可以启发我吗?谢谢。

mar*_*tin 5

你导入错误了concat。您想将其用作运算符,因此必须导入

import { concat } from 'rxjs/operators';
Run Code Online (Sandbox Code Playgroud)

concat从中导入'rxjs'的是可观察的创建方法,而不是运算符。

这将在 RxJS 的更高版本中重命名,因为它太具有误导性: https: //github.com/ReactiveX/rxjs/issues/3927