我很难理解 RxJs。我很难理解 5 和 6 之间的重大变化。
我有以下代码和以下问题。
- .interval 方法不再可从 Observable 获得
- .interval 不再提供 .switchMap 方法
我检查了变更日志和修复重大变更的建议,但我无法理清我需要做什么。按照我的理解,我的代码很旧并且没有使用管道运算符,但这就是我所能弄清楚的全部内容。
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));
最佳答案
参见 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
,但这只是一个兼容层,您应该真正使用管道语法。
关于javascript - 从 RxJs 5 迁移到 6 : switchMap and Interval broken,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50592218/