加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

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

发布时间:2020-12-17 07:20:46 所属栏目:安全 来源:网络整理
导读:我有一个组件从订阅商店获取其数据. this.store.select('somedata').subscribe((state: any) = { this.somedata = state.data;}); 我想在组件不再使用时取消订阅此订阅,在我订阅某些observable的其他地方,如下所示: this.service.data.subscribe( (result:
我有一个组件从订阅商店获取其数据.
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();
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读