繁体   English   中英

RxJs结合http请求

[英]RxJs combine http request

我正在尝试在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定义为返回可观察值的东西

b(accountId userId): Observable<any> {
 return http.get<any>(... +  '/' + accountId + '/' + userId)
}

您可以使用forkJoin合并多个订阅。

forkJoin([a, b]).subscribe((res) => {
  // res[0] should be the response of `a`
  // res[1] should be the response of `b`
});

您只需通过aResponse将数据向下传递。

a = http.get<any>(...);
b = http.get<any>(... +  '/' + accountId + '/' + userId})
const getBFromA$ = a.pipe(
    switchMap((aResponse) => {
        const accountId = aResponse.accountId;
        const userId = aResponse.userId;
        return http.get<any>(... +  '/' + accountId + '/' + userId})
    }).subscribe((bResponseOnly) => { // do something if you want to })

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM