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

Tkw*_*123 5 rxjs apollo graphql angular

我正在使用 apollo-angular 构建一个 angular (4.x) 应用程序,我想知道如何取消订阅 apollo observables(如果你需要的话)。

我正在尝试通过创建查询来遵循此响应中的指导:

this.query = this.apollo.watchQuery<LatestReportQueryResponse>({
  fetchPolicy: 'network-only',
  query: myQuery
});
Run Code Online (Sandbox Code Playgroud)

分配一个新的主题:

  private ngUnsubscribe: Subject<void> = new Subject<void>();
Run Code Online (Sandbox Code Playgroud)

订阅查询:

this.query.takeUntil(this.ngUnsubscribe).subscribe(({ data }) => {...}
Run Code Online (Sandbox Code Playgroud)

然后在一个onDestroy事件循环中销毁所有活动的可观察对象,例如:

ngOnDestroy() {
    this.ngUnsubscribe.next();
    this.ngUnsubscribe.complete();
  }
Run Code Online (Sandbox Code Playgroud)

添加 后.takeUntil(this.ngUnsubscribe),我遇到了以下 lint 错误:

“主题”类型的参数不可分配给“可观察”类型的参数。

或者当我尝试手动取消订阅 ApolloQueryObservable 时,我得到:

“ApolloQueryObservable”类型不存在“取消订阅”属性。您指的是 'subscribe' 吗?

apollo observables 是否需要取消订阅?

Loc*_*0_0 5

的返回值this.query.takeUntil(this.ngUnsubscribe).subscribe(...)应该为您提供取消订阅功能。

订阅和保存取消订阅功能:

this.unsubscribe = this.query.takeUntil(this.ngUnsubscribe).subscribe(...)

onDestroy事件循环中,调用该函数:

this.unsubscribe()