我有一个组件从订阅商店获取其数据.
- 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();
- }