timer.unsubscribe不是一个函数Angular2 [英] timer.unsubscribe is not a function Angular2

查看:137
本文介绍了timer.unsubscribe不是一个函数Angular2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图制作一个简单的计时器,需要在某些if条件下将其中断.但是总是出现错误EXCEPTION: timer.unsubscribe is not a function.

Tried to make a simple timer and need to break it on some if condition. But always got an error EXCEPTION: timer.unsubscribe is not a function.

我在做什么错了?

    let timer:any = Observable.timer(0,1000);
    timer.subscribe(data => {
            console.log(this.itemsRatedList);
            if(data == 5) timer.unsubscribe();
        });

推荐答案

应为:

let timer:any = Observable.timer(0,1000);
let subscription = timer.subscribe(data => {
  console.log(this.itemsRatedList);
  if(data == 5) 
    subscription.unsubscribe();
});

您不能unsubscribe一个Observable,只能一个Subscription.

You can't unsubscribe an Observable, only a Subscription.

> 柱塞示例

这篇关于timer.unsubscribe不是一个函数Angular2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆