首页 > 解决方案 > RxJs 结合 http 请求

问题描述

我正在尝试在 rxjs 中组合两个 HTTP 请求来帮助组合请求,但是第二个请求的参数(accountId,userId)在第一个请求的响应中。

a = http.get<any>(...);
b = http.get<any>(... +  '/' + accountId + '/' + userId})

concat(a, b).subscribe(res => {});

标签: javascriptangulartypescriptrxjs

解决方案


您可以使用 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)
}

推荐阅读