代码之家  ›  专栏  ›  技术社区  ›  prabhat gundepalli

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

  •  0
  • prabhat gundepalli  · 技术社区  · 8 年前

    我想减去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

    我们如何使它在RXJS6中工作?

    在尝试combinelatest的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 回复  |  直到 8 年前
        1
  •  0
  •   prabhat gundepalli    8 年前

    我能让它在帮助下工作(通过gitter@dorus和guillaumeunice)

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

    观察:作为一个开发人员,当一个简单的算术运算是如此复杂的时候,这是很奇怪的。

    推荐文章