RxJ观测到几个可观测值



在RxJs的旧版本中,我使用了以下方法:

combineLatest([
this.state.select(selector1),
this.stage.select(selector2)
])
.pipe(...)
.subscribe(values => {
const parameter: any = values[0];
const data: any = values[1];
);

在我的新项目中,我使用RxJs 7.5,这已经不可能了。

combineLatestWith([
this.userData.controls["name"].valueChanges,
this.userData.controls["lastName"].valueChanges,
this.userData.controls["age"].valueChanges,
this.userData.controls["birthDate"].valueChanges])
.subscribe((result:any) => {
);

编译器说:";类型"OperatorFunction<"上不存在属性"subscribe";未知,[未知,任意]>'">

下面剪下来的看起来有点难看:

this.userData.controls["name"].valueChanges.combineLatestWith([
this.userData.controls["lastName"].valueChanges,
this.userData.controls["age"].valueChanges,
this.userData.controls["birthDate"].valueChanges])
.subscribe((result: any) => {
console.log(result);
});

有没有其他类似于第一种方法的解决方案?

您使用了错误的combineLatest()导入。由于RxJS 7.0,创建运算符被称为combineLatest(),并且是从'rxjs'导入的(这是您想要使用的(。

import { combineLatest } from 'rxjs';
...
combineLatest([firstTimer, secondTimer]).subscribe(...);

combineLatestWith()是一个pipable运算符,用于RxJS链内部(因此是错误(。

import { combineLatestWith } from 'rxjs';
...
input1Changes$.pipe(
combineLatestWith(input2Changes$),
map(([e1, e2]) => ...)
)

最新更新