下面是一个代码示例。 请给我一个示例,如何使用诸如mergeMap / combine observables之类的功能来改进此代码。
ngOnInit() {
this.route.params
.pipe(takeUntil(this.destroy$))
.subscribe((params: any) => {
this.getRound(params.id)
.pipe(takeUntil(this.destroy$))
.subscribe((round: any) => {
this.round = round;
this.getStats(params.id, round.required_tickets)
.pipe(takeUntil(this.destroy$))
.subscribe((stats: any) => {
this.stats = stats;
});
});
});
}
答案 0 :(得分:2)
您可以尝试下面的代码
ngOnInit() {
this.route.params
.pipe(
takeUntil(this.destroy$),
switchMap((params: any) => {
this.params = params
return this.getRound(params.id)
}),
switchMap((round: any) => {
this.round = round;
return this.getStats(this.params.id, round.required_tickets)
})
)
.subscribe((stats: any) => {
this.stats = stats;
});
}
另外,我绝对不会使用任何类型