[英]Managing multiple RxJs Observables on a component
Below is a code snippet of a component in my application where I subscribe to multiple observables and load data [an api call] after I get values respectively.下面是我的应用程序中一个组件的代码片段,其中我订阅了多个 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 works for me in this scenario, however the loadData is being called multiple times.在这种情况下,CombineLatest 对我有用,但是 loadData 被多次调用。 I believe this is happening because of the observables emitting values after they update.我相信这是因为 observables 在更新后会发出值。 The initial values for [studentId, teacherId, resultId, reportId, yearId] from the service are set to 0, to cater combineLatest.服务中的 [studentId、teacherId、resultId、reportId、yearId] 的初始值设置为 0,以满足 combineLatest 的需求。
Is there any way I can call LoadData method [api call] after all the 5 values are received [not the intialvalues of 0]?在收到所有 5 个值后,有什么方法可以调用 LoadData 方法 [api call] [不是 0 的初始值]? Something like onComplete?像 onComplete 这样的东西?
用combineLatest
替换forkJoin
,这将在发出之前等待所有 observable 完成。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.