如何在rxjs中取消订阅我漂亮的TimerObservable

时间:2016-09-30 11:44:00

标签: rxjs rxjs5

我一直收到取消订阅不是函数的错误。我正在使用rxjs@5.0.0-beta.12。

这些是我的导入声明:

import { TimerObservable } from 'rxjs/observable/TimerObservable';
import 'rxjs/add/operator/take';

这是有问题的TimerObservable:

countdown = TimerObservable.create(0, 1000).take(6);

这将启动计时器:

startCountdownTimer(): void {
    this.countdown.subscribe(
        i => this.timeRemaining = (5 - i).toString(),
        null,
        () => this.toDelete = -1)
}

这个,这不起作用:

cancelCountdownTimer(): void {
    this.countdown.unsubscribe();
}

1 个答案:

答案 0 :(得分:0)

subscribe的来电者收到subscription

startCountdownTimer(): void {
    this.subscription = this.countdown.subscribe(
        i => this.timeRemaining = (5 - i).toString(),
        null,
        () => this.toDelete = -1);
}

unsubscribe方法:

cancelCountdownTimer(): void {
    this.subscription.unsubscribe();
}