简体   繁体   English

Rx.js取消订阅给了错误

[英]Rx.js unsuscribe giving gerror

I'm currently starting out with observables in JS, and I've hit a total roadblock with what seems like something really basic. 我目前从JS中的可观察对象开始,并且遇到了一些看起来非常基础的难题。 I've created the super simple example below, which whilst useless by itself, looks to me like it should work, but it doesn't. 我在下面创建了一个超级简单的示例,尽管它本身毫无用处,但在我看来它应该可以工作,但事实并非如此。 the final line throws an error. 最后一行抛出错误。

const obs1 = Rx.Observable.timer(2000)
    .map(() => {return Math.random() > 0.5});

const subscription = obs1.subscribe(
   (v) => {
      console.log('hello world: ', v)
   });

subscription.unsubscribe();

The error is 错误是

TypeError: subscription.unsubscribe is not a function TypeError:subscription.unsubscribe不是函数

My question is basically, why can't I unsubscribe from the above observer? 我的问题基本上是,为什么我不能退订上述观察者? Or how should I unsubscribe from it? 还是我应该取消订阅?

You might be using Rx v4.x, isn't it? 您可能正在使用Rx v4.x,不是吗? In that case, the method is dispose not unsubscribe 在这种情况下,该方法是disposeunsubscribe

subscription.dispose();

Your two options are to either: 您有两种选择:

  • upgrade to rxjs v5; 升级到rxjs v5;
  • replace unsubscribe with dispose . dispose代替unsubscribe

If you codebase hasn't mush code yet, I suggest you upgrade to v5 :) 如果您的代码库尚未输入代码,建议您升级到v5 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM