[英]Managing multiple RxJs Observables on a component
下面是我的應用程序中一個組件的代碼片段,其中我訂閱了多個 observable 並在我分別獲取值后加載數據 [一個 api 調用]。
ngOnInit() {
this.combinedObservable$ = combineLatest(
this.service1.getStudentId(),
this.service2.getTeacherId(),
this.service3.getResultId(),
this.service4.getReportId(),
this.service5.getYearId(),
).subscribe(value => {
this.studentId = value[0];
this.teacherId = value[1];
this.resultId = value[2];
this.reportId = value[3];
this.yearId = value[4];
// Load Data for the page by making an api call
this.loadData(value[0], value[1], value[2], value[3], value[4]);
});
}
ngOnDestroy() {
this.combinedObservable$.unsubscribe();
}
在這種情況下,CombineLatest 對我有用,但是 loadData 被多次調用。 我相信這是因為 observables 在更新后會發出值。 服務中的 [studentId、teacherId、resultId、reportId、yearId] 的初始值設置為 0,以滿足 combineLatest 的需求。
在收到所有 5 個值后,有什么方法可以調用 LoadData 方法 [api call] [不是 0 的初始值]? 像 onComplete 這樣的東西?
用combineLatest
替換forkJoin
,這將在發出之前等待所有 observable 完成。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.