从RxJs 5迁移到6:switchMap和Interval已损坏

时间:2018-05-29 20:10:46

标签: javascript rxjs rxjs5 angular6 rxjs6

我很难理解RxJs。 5到6之间的突破性变化对我来说很难理解。

我有以下代码和以下问题。

  1. 。可以从Observable
  2. 获取.interval方法
  3. .switchMap方法不再可用于.interval
  4. 我检查了更改日志以及修复重大更改的建议,但我无法理清我需要做什么。我理解它的方式,我的代码是旧的,不使用管道操作符,但这是我能够弄清楚的。

    let polling = Observable.interval(2000)
    .switchMap(() => this.http.get(this.videoStatusURL + this.taskID))
    .subscribe(
      (data) => {              
        if (data["state"] === "SUCCESS") {
          //get final video here
          console.log("polling succeeded");
          this.downloadFinalVideo();
          polling.unsubscribe();
        }            
      },
      error => this.handleError(error));
    

1 个答案:

答案 0 :(得分:1)

请参阅pipe syntax in the migration doc部分,必须通过调用.pipe()来调用运算符,因此您需要执行类似

的操作
import { interval } from 'rxjs';
import { switchMap } from 'rxjs/operators';

let polling = interval(2000)
.pipe(switchMap(() => this.http.get(this.videoStatusURL + this.taskID)))
.subscribe(
  (data) => {              
    if (data["state"] === "SUCCESS") {
      //get final video here
      console.log("polling succeeded");
      this.downloadFinalVideo();
      polling.unsubscribe();
    }            
  },
  error => this.handleError(error));

或者,您可以安装rxjs-compat,但这只是一个兼容层,您应该使用管道语法。