简体   繁体   English

无法读取未定义的属性'unsubscribe'(rxJS - 订阅)

[英]Cannot read property 'unsubscribe' of undefined (rxJS - Subscription)

I am trying to get a grip of subscriptions and observables in rxJS. 我试图抓住rxJS中的订阅和可观察量。

I want to change the interval of an Observable by unsubscribing and then resubscribe using the new interval setting. 我想通过取消订阅来更改Observable的间隔,然后使用新的间隔设置重新订阅。

Should be really simple but since I'm a beginner in this area I could need some assistance. 应该是非常简单,但由于我是这个领域的初学者,我可能需要一些帮助。

See this plunk 看到这个插件

export class AppComponent implements OnInit {
  title = 'Observable Interval - Changing interval';
  currentTime: any;
  refreshInterval: number = 1000;
  private subscription: Subscription;

  constructor(private timeService: TimeService) {
  }

  clicked($event) {
    console.log('new refreshInterval: ' + this.refreshInterval);

    // Here I would like to unsubscribe to the subscription 
    // and then resubscribe using the new interval. 
    // However using below statement causes a 
    // TypeError: Cannot read property 'unsubscribe' of undefined
    this.subscription.unsubscribe();
    this.getTime();
  }

  // with this implementation changing the refreshInterval won't have any affect. 
  getTime() {
            this.timeService.getTime(this.refreshInterval)
              .subscribe(t => {
                this.currentTime = t;
              }
            );
    }

  ngOnInit() {
    this.subscription = this.getTime();
    console.log(this.subscription);
    console.log('refreshing each ' + this.refreshInterval + ' ms');
  }
}

You need to return the subscription within the getTime method: 您需要在getTime方法中返回订阅:

getTime() {
  return this.timeService.getTime(this.refreshInterval) // <-----
          .subscribe(t => {
            this.currentTime = t;
          }
        );
}

In your case, nothing is returned. 在您的情况下,没有任何返回。 That's why you have an undefined error... 这就是为什么你有一个未定义的错误...

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

相关问题 Angular 2 Http,RxJS无法读取未定义的属性“取消订阅” - Angular 2 Http, RxJS Cannot read property 'unsubscribe' of undefined 无法读取属性“添加”。 Rxjs订阅 - Cannot read property 'add'. Rxjs Subscription 无法读取未定义的 Angular4 的属性“取消订阅” - Cannot read property 'unsubscribe' of undefined Angular4 Angular 2:无法读取未定义的属性“取消订阅” - Angular 2: Cannot read property 'unsubscribe' of undefined 无法读取未定义的属性退订:Angular2测试 - Cannot read property unsubscribe of undefined: Angular2 testing 单元测试主题:无法读取未定义的属性“取消订阅” - Unit test Subject: Cannot read property 'unsubscribe' of undefined Angular 2单元测试“无法读取未定义的属性&#39;取消订阅” - Angular 2 unit testing “Cannot read property 'unsubscribe' of undefined” ngOnDestroy Uncaught(承诺):TypeError:无法读取未定义的属性“取消订阅” - ngOnDestroy Uncaught (in promise): TypeError: Cannot read property 'unsubscribe' of undefined 使用RxJS的Angular 6错误:无法读取未定义的属性“调用” - Angular 6 Error using RxJS: Cannot read property 'call' of undefined Angular2 + RxJS-无法读取未定义的下一个属性 - Angular2 + RxJS - cannot read property next of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM