微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

angular – 如何取消订阅ngrx / store?

我有一个组件从订阅商店获取其数据.
this.store.select('somedata').subscribe((state: any) => {
  this.somedata = state.data;
});

我想在组件不再使用时取消订阅订阅,在我订阅某些observable的其他地方,如下所示:

this.service.data.subscribe(
   (result: any) => {//data}
);

我在ngOnOnDestroy上取消订阅,如下所示:

ngOnDestroy(){
   this.service.data.unsubscribe();
}

但是对于商店我无法做到,它给了我错误

Property 'unsubscribe' does not exist on type 'Store<State>'
订阅时,您将收到订阅对象,您可以调用unsubscribe()
const subscription = this.store.select('somedata').subscribe((state: any) => {
  this.somedata = state.data;
});
// later
subscription.unsubscribe();

要么

ngOnInit(){
 this.someDataSubscription = this.store.select('somedata').subscribe((state: any) => {
  this.somedata = state.data;
 });
}

ngOnDestroy(){
  this.someDataSubscription.unsubscribe();
}

原文地址:https://www.jb51.cc/angularjs/141697.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐