angular - 你如何取消订阅 angular 中的 apollo observable?

标签 angular rxjs graphql apollo

我正在使用 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/

相关文章:

javascript - ApplicationRef.isStable 未执行

python - 如何将 graphql 模式导入 graphene-python 程序?

javascript - `reduce/scan` 的累加器函数中的可观察类型是否匹配?

html - 如何禁用 HTML 元素中的换行?

angular - 如何将 ngrx-store-freeze 与 nx 工作区一起使用

azure - 带转换的 Angular2 配置数据

angular - 如何取消订阅/停止 Observable?

lambda - 如何在apollo-server-lambda中处理cookie

javascript - CORS 阻止 GraphQL Yoga 中的突变

javascript - Angular 和 RxJs 结合了两个 http 请求