2017-09-05 67 views
2

我正在構建一個使用apollo-angular的角(4.x)應用程序,並且我想知道如何取消訂閱apollo observables(如果您需要的話)。你如何取消訂閱apollo可觀察角度?

我試圖通過創建一個查詢跟隨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 }) => {...} 

然後用類似下面的東西摧毀所有活動週期中的活動週期:

ngOnDestroy() { 
    this.ngUnsubscribe.next(); 
    this.ngUnsubscribe.complete(); 
    } 

加入.takeUntil(this.ngUnsubscribe)後,我遇到這樣的錯誤皮棉:類型的

參數「主題」是不能分配給類型「可觀測」的參數。

或當我嘗試手動退訂ApolloQueryObservable,我得到:

屬性「退訂」上不存在型「ApolloQueryObservable」。你的意思是'訂閱'嗎?

是否需要取消訂閱apollo observables?

回答

0

返回值this.query.takeUntil(this.ngUnsubscribe).subscribe(...)應該給你退訂功能。

訂閱和退訂保存功能:

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

onDestroy事件循環,調用該函數:

this.unsubscribe()

+0

感謝您的答覆!但是,當我在「this.query」之前(在takeunti())打字稿投訴之前:「''''這個''''類型'ApolloQueryObservable '的上下文不能分配給方法的Observable類型的'this'有什麼方法可以確認observable被完成/銷燬嗎? – Tkwon123

+0

對不起,我不確定它是否可以被確認。也許它是寫在[ API](http://dev.apollodata.com/angular2) –