I have a component which gets its data from subscribing to a store.
this.store.select('somedata').subscribe((state: any) => {
this.somedata = state.data;
});
I want to unsubscribe from this subscription when component is no more, in other places where I am subscribing to some observable, something like this:
this.service.data.subscribe(
(result: any) => {//data}
);
I unsubscribed it on ngOnOnDestroy, like this:
ngOnDestroy(){
this.service.data.unsubscribe();
}
But in case of store I'm not able to, it gives me error:
Property 'unsubscribe' does not exist on type 'Store<State>'
When you subscribe you will receive a subscription object on it you can call unsubscribe()
const subscription = this.store.select('somedata').subscribe((state: any) => {
this.somedata = state.data;
});
// later
subscription.unsubscribe();
or
ngOnInit(){
this.someDataSubscription = this.store.select('somedata').subscribe((state: any) => {
this.somedata = state.data;
});
}
ngOnDestroy(){
this.someDataSubscription.unsubscribe();
}