首页 文章

如何在RXJS 6中减去2个可观测值

提问于
浏览
0

我想减去2个可观察的数字

this.num1:Observable<number>
this.num2:Observable<number>
this.num3:Observable<number>

this.num1 = this.store.select(getNum1Count);
this.num2 = this.store.select(getNum2Count);
// this.num3 = difference of this.num1 and this.num2

在RXJS5中,我正在执行以下命令

this.num3 = Observable.combineLatest(this.num1,this.num2,(c1,c2)=> Math.abs(c1 - c2));

但是在RXJS6中,不推荐使用combineLatest:不推荐使用静态combineLatest as shown here

我们如何让它在RXJS 6中工作?

在试用combineJatest的RXJS6格式时,

combineLatest(this.num3,this.num2, this.num1, (c1,c2) =>  Math.abs(c1 - c2 )),filter(x => x !== NaN);

我收到错误The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type.

1 回答

  • 0

    我能够帮助它(通过gitter @Dorus和@GuillaumeUnice)

    this.num3 = this.num1.pipe(combineLatest(this.num2),map(([n1,n2]) =>  Math.abs(n1 - n2)));
    

    观察:作为一个开发人员,当一个简单的算术运算这么复杂时,这很奇怪 .

相关问题