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

前端之家收集整理的这篇文章主要介绍了angular – 如何取消订阅ngrx / store?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个组件从订阅商店获取其数据.
  1. this.store.select('somedata').subscribe((state: any) => {
  2. this.somedata = state.data;
  3. });

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

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

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

  1. ngOnDestroy(){
  2. this.service.data.unsubscribe();
  3. }

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

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

要么

  1. ngOnInit(){
  2. this.someDataSubscription = this.store.select('somedata').subscribe((state: any) => {
  3. this.somedata = state.data;
  4. });
  5. }
  6.  
  7. ngOnDestroy(){
  8. this.someDataSubscription.unsubscribe();
  9. }

猜你在找的Angularjs相关文章