Javascript rxjs switchmap observable,保存变量以供最终
我需要知道解决这个问题的好方法 我正在使用switchMaps调用三个服务concat,但我需要一个变量first service for the last service。这样做的最佳实践是什么 例如:Javascript rxjs switchmap observable,保存变量以供最终,javascript,rxjs,Javascript,Rxjs,我需要知道解决这个问题的好方法 我正在使用switchMaps调用三个服务concat,但我需要一个变量first service for the last service。这样做的最佳实践是什么 例如: this.session.account.get() .switchMap(account => this.employerApi.get(account.accountId)) .switchMap(employer => this.addressApi.get(employer
this.session.account.get()
.switchMap(account => this.employerApi.get(account.accountId))
.switchMap(employer => this.addressApi.get(employer.addressId))
.filter(address => address.numer % 2)
.subscribe(address => console.log(¿¿¿¿¿account.name?????, address.name));
感谢您的帮助最简单的方法是边走边聚合值:
this.session.account.get()
.switchMap(account =>
this.employerApi.get(account.accountId).map(employer => ({employer, account}))
.switchMap(data =>
this.addressApi.get(employer.addressId).map(address => ({...data, address}))
.filter(data => data.address.number % 2)
.subscribe(...)
不确定这是最好的方法,但这里有一种方法:
this.session.account.get()
.switchMap(account => zip(Observable.from(account), this.employerApi.get(account.accountId))
.switchMap((account, employer) => zip(Observable.from(account), this.addressApi.get(employer.addressId))
.filter((account,address) => address.numer % 2)
.subscribe((account,address) => console.log(¿¿¿¿¿account.name?????, address.name));
然而,就最佳实践而言,我认为您需要的是后端的序列化程序,它以json格式返回结果,比如:
{“account”:account\u object,“address”:address\u object}
我认为使用zip函数不是很优雅,我更喜欢构建json。谢谢我相信这可能是最好的方法,谢谢!