鉴于以下内容:
this.activatedRoute.parent.params
.subscribe(params => {
let fooId = +params['fooId'];
this.activatedRoute.params
.subscribe(params => {
let barId = +params['barId'];
this.fooBarService.getFooBars(fooId, barId)
.subscribe(fooBars => {
this.fooBars = fooBars;
});
});
});
可以使用以下内容简化部分嵌套:
this.activatedRoute.parent.params
.subscribe(params => {
let foo = +params['fooId'];
this.activatedRoute.params
.switchMap(params => this.fooBarService.getFooBars(fooId, +params['barId']))
.subscribe(fooBars => {
this.fooBars = fooBars;
});
});
但是可以展平剩余的嵌套订阅吗?
答案 0 :(得分:0)
使用combineLatest运算符:
Observable
.combineLatest(this.activatedRoute.parent.params, this.activatedRoute.params)
.switchMap(data => this.fooBarService.getFooBars(+(data[0])['fooId'], +(data[1])['barId']))
.subscribe(fooBars => {
this.fooBars = fooBars;
});