http请求中的异步选项,角度为2

问题描述 投票:0回答:1

我使用angular 2 http class https://angular.io/docs/js/latest/api/http/index/Http-class.html发送帖子和获取请求。现在,我想同步使用发送请求。即,在第一次请求的响应之后发送第二请求。但在角度2文档中没有可用的选项。所以,

如何同步发送多个请求?

在jquery中,ajax async选项可用于处理此类问题。我正在寻找角度2中的类似选项。

ajax http angular
1个回答
1
投票

您可以与observables同步/等待。有很多方法可以得到这个例子,这是一个选择。如果你想要更多检查:https://www.learnrxjs.io/operators/transformation/switchmap.html

你也有MergeMap和其他人

一个例子:

this.http.get(url1)
.switchMap(
 (response1: Response) => {
          return this.http.url(url2);
})
.subscribe(
   (response2: Response) => {},
   (error: Response) => {},
   () => console.log('completed')
);

希望它能帮到你!

© www.soinside.com 2019 - 2024. All rights reserved.