javascript - RxJs 结合 http 请求
问题描述
我正在尝试在 rxjs 中组合两个 HTTP 请求来帮助组合请求,但是第二个请求的参数(accountId,userId)在第一个请求的响应中。
a = http.get<any>(...);
b = http.get<any>(... + '/' + accountId + '/' + userId})
concat(a, b).subscribe(res => {});
解决方案
您可以使用 mergeMap,因为您说 accountId 和 userId 在第一个 api 请求的响应中。
a.pipe(
mergeMap(data => b(data.accountId,data.userId))
).subscribe(response => console.log(response));
您应该将 b 定义为返回 observable 的东西
b(accountId userId): Observable<any> {
return http.get<any>(... + '/' + accountId + '/' + userId)
}
推荐阅读
- xamarin - 我可以使用 aab 来减少带有 Mac OS / XF 的 Android 包的大小吗?
- python-3.x - Tensorflow 显然安装正常,但检查失败
- android - 我从 AVD 手动卸载了我的应用程序,我必须手动重新安装它吗?
- android - 如何定义另一个列表长度相同的默认列表
- excel - 如何创建代码来检测是否有任何工作表受到保护?
- c# - 无法下载 application/vnd.google-apps.site 文档
- ruby-on-rails - 从 ThinkingSphinx 迁移到 Elasticsearch on rails
- android - 无法导入包 com.google.android.exoplayer2.ext.mediasession.MediaSessionConnector
- node.js - 多米诺授权node.js
- python - 在 Python ftplib 中创建并上传到 FTP 的文件为空