我正在寻找符合给定标准的文件。在这里,我搜索surveys
和exams
,它们通过订阅两个不同的请求存储在两个不同的变量中。
getEntities() {
this.submissionsService.getSubmissions(findDocuments({
'user.name': this.userService.get().name,
type: 'survey',
status: 'pending'
})).subscribe((surveys) => {
this.surveys = surveys;
});
this.submissionsService.getSubmissions(findDocuments({
'user.name': this.userService.get().name,
type: 'exam',
status: 'pending'
})).subscribe((exams) => {
this.exams = exams;
});
}
如何将两个可观察到的数据保存在一个服务请求上而不是发出两个单独的请求?
谢谢!
发布于 2019-01-29 05:32:14
您可以在rxjs中使用forkjoin
。
forkjoin:当您有一组可观察的并且只关心每个操作符的最终发出值时,最好使用这个操作符。这方面的一个常见用例是,如果您希望在页面加载(或其他一些事件)上发出多个请求,并且只希望在收到所有人的响应时才采取行动。
getEntites() {
const surveysObservable = this.submissionsService.getSubmissions(findDocuments({
'user.name': this.userService.get().name,
type: 'survey',
status: 'pending'
}));
const examsObservable = this.submissionsService.getSubmissions(findDocuments({
'user.name': this.userService.get().name,
type: 'exam',
status: 'pending'
}));
Observable.forkJoin([surveysObservable , examsObservable]).subscribe(results => {
this.surveys = results[0];
this.exams = results[1];
});
}
检查一下这个工作的stackblitz。了解有关forkjoin 这里。的更多信息
https://stackoverflow.com/questions/54414020
复制相似问题