【发布时间】:2019-12-02 07:37:54
【问题描述】:
在 rxjs 中使用 Angular 来显示第一个 observable 的结果并在其他 observable 完成时组合数据的最佳方式是什么?
例子:
@Component({
selector: 'app-component',
template: `
<div *ngFor="let group of groups$ | async">
<div *ngFor="let thisItem of group.theseItems">
...
</div>
<div *ngFor="let thatItem of group.thoseItems">
...
</div>
</div>
`
})
export class AppComponent implements OnInit {
...
ngOnInit() {
this.groups$ = this.http.get<IThisItem[]>('api/theseItems').pipe(
map(theseItems => {
return theseItems.groupBy('groupCode');
})
);
// combine these results? This operation can take 5 seconds
this.groups$$ = this.http.get<IThatItem[]>('api/thoseItems').pipe(
map(thoseItems => {
return thoseItems.groupBy('groupCode');
})
);
}
}
我知道可以通过订阅两者并合并结果来完成。但是是否可以为此使用管道运算符,并使用async 管道?
【问题讨论】:
-
为了清楚起见,你想用这些项目获得一次发射,然后你想用这些项目与那些项目组合一次发射?
-
是的,但理论上这可能是相反的;
thoseItems被发射之前theseItems被发射
标签: angular rxjs rxjs-observables