我正在使用 apollo-angular 构建一个 angular (4.x) 应用程序,我想知道如何取消订阅 apollo observables(如果你需要的话)。
我正在尝试遵循 this response 中的指导通过创建查询:
this.query = this.apollo.watchQuery<LatestReportQueryResponse>({
fetchPolicy: 'network-only',
query: myQuery
});
分配一个新的主题:
private ngUnsubscribe: Subject<void> = new Subject<void>();
订阅查询:
this.query.takeUntil(this.ngUnsubscribe).subscribe(({ data }) => {...}
然后销毁
onDestroy
上的所有事件可观察对象事件循环类似于:ngOnDestroy() {
this.ngUnsubscribe.next();
this.ngUnsubscribe.complete();
}
添加后
.takeUntil(this.ngUnsubscribe)
,我遇到了以下 lint 错误:Argument of type 'Subject' is not assignable to parameter of type 'Observable'.
或者当我尝试手动取消订阅 ApolloQueryObservable 时,我得到:
Property 'unsubscribe' does not exist on type 'ApolloQueryObservable'. Did you mean 'subscribe'?
apollo observables 是否需要取消订阅?
最佳答案
this.query.takeUntil(this.ngUnsubscribe).subscribe(...)
的返回值应该给你取消订阅功能。
订阅和保存取消订阅功能:this.unsubscribe = this.query.takeUntil(this.ngUnsubscribe).subscribe(...)
在 onDestroy
事件循环,调用函数:this.unsubscribe()
关于angular - 你如何取消订阅 angular 中的 apollo observable?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46062915/