Angular 角度及;RxJS-使用.mergeMap组合多个XHR服务调用
我最近问了一个问题 得到了一个很好的答案,但是随着我的系统的发展,我需要修改我的代码,而我的实现无法工作 在我的系统中,我在关闭操作后订阅了一个Angular Material对话框,我获取结果,过滤该结果,然后调用检索数据的服务(使用REST/XHR)。解决此问题后,我将获取并修改一些返回的数据,并返回另一个服务方法。mergeMap用于防止一系列嵌套的Angular 角度及;RxJS-使用.mergeMap组合多个XHR服务调用,angular,typescript,rxjs,Angular,Typescript,Rxjs,我最近问了一个问题 得到了一个很好的答案,但是随着我的系统的发展,我需要修改我的代码,而我的实现无法工作 在我的系统中,我在关闭操作后订阅了一个Angular Material对话框,我获取结果,过滤该结果,然后调用检索数据的服务(使用REST/XHR)。解决此问题后,我将获取并修改一些返回的数据,并返回另一个服务方法。mergeMap用于防止一系列嵌套的.subscribes dialogRef .afterClosed() .filter((result) => typeof resu
.subscribe
s
dialogRef
.afterClosed()
.filter((result) => typeof result === 'string')
.mergeMap((result) => this.myService.getuser(this.id).mergeMap((user: User) => {
const mergedObj = Object.assign({}, user, {LifeEventDate: this.dateService.convertToCorrectFormat(result)});
return this.myService.updateUser(this.id, mergedObj);
}))
.do(() => {
// run some actions in the Component...
})
.subscribe();
现在,我希望从this.myService.getuser
获取一些返回的数据,并调用相同的服务,但调用另一个方法this.myService.logSomething
(它也进行REST/XHR调用)。方法this.myService.logSomething
不返回任何必要的数据,当完成时,它只返回一个对象{success:true}
,尽管在出现错误时引入.catch可能是谨慎的。我想将其添加到上面的代码中,将其放在第二个mergeMap中并不起作用,因为在Chrome开发工具中查找时,没有对此提出任何网络请求。例如
dialogRef
.afterClosed()
.filter((result) => typeof result === 'string')
.mergeMap((result) => this.myService.getuser(this.id).mergeMap((user: User) => {
/* NEW CODE */
// I need to take some of the data returned from the returned user above
this.myService.logSomething({user:user.id, something: user.something, somethingElse: user.somethingElse});
const mergedObj = Object.assign({}, user, {LifeEventDate: this.dateService.convertToCorrectFormat(result)});
return this.myService.updateUser(this.id, mergedObj);
}))
.do(() => {
// run some actions in the Component...
})
.subscribe();
我不确定在何处或如何拨打此新服务电话。嵌套在第一个.mergeMap
中的两个服务方法都需要来自第一个服务调用的数据,如何向代码中添加第三个服务调用
很抱歉,如果问题结构不好、没有意义或不清楚,请让我知道,我会重新表述。我认为您可以链接多个mergeMap
(或concatMap
)调用,并将结果映射到原始用户
对象(我假设logSomething()
返回一个可观察的对象):
为什么要使用mergeMap
?我想试试这样的东西:
dialogRef.afterClosed().filter(...)
.switchMap(e => this.mysService.getUser(this.id))
.switchMap(user => Observable.forkJoin(
this.myService.logSomething(user),
(user) => {
const mergedObj = Object.assign();
return this.myService.updateUser(this.id, mergedObj)
},
(a,b) => b //Selector: return a to pass along the first observable's result
))
.do(...)
.subscribe(...)
你能帮忙吗?
dialogRef.afterClosed().filter(...)
.switchMap(e => this.mysService.getUser(this.id))
.switchMap(user => Observable.forkJoin(
this.myService.logSomething(user),
(user) => {
const mergedObj = Object.assign();
return this.myService.updateUser(this.id, mergedObj)
},
(a,b) => b //Selector: return a to pass along the first observable's result
))
.do(...)
.subscribe(...)