【问题标题】:Migrating from RxJs 5 to 6: switchMap and Interval broken从 RxJs 5 迁移到 6:switchMap 和 Interval 损坏
【发布时间】:2018-11-08 13:45:20
【问题描述】:

我很难理解 RxJs。我很难理解 5 和 6 之间的重大变化。

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

  1. .interval 方法不再可从 Observable 获得
  2. .switchMap 方法在 .interval 中不再可用

我查看了更改日志和修复重大更改的建议,但我无法确定我需要做什么。我理解它的方式是,我拥有的代码很旧并且不使用管道运算符,但这就是我能够弄清楚的全部。

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));

【问题讨论】:

标签: javascript rxjs rxjs5 angular6 rxjs6


【解决方案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,但这只是一个兼容层,您应该真正使用管道语法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-12-12
    • 2018-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-27
    • 1970-01-01
    相关资源
    最近更新 更多