简体   繁体   English

Angular 4 Http Chain请求

[英]Angular 4 Http Chain Requests

I'm having trouble figuring out how to use the result of an Http request to make another Http request. 我无法弄清楚如何使用Http请求的结果来发出另一个Http请求。 Let's make it clear with example; 让我们用例子说清楚; Firstly I getting data from this api: 首先我从这个api获取数据:

[{"id": 1}]

Secondly I should getting data from another api and result is : 其次我应该从另一个api获取数据,结果是:

[{"id": 1, "name": "id labore ex et quam laborum", }]

So I should check api İf first data id =1 then I should post "id labore ex et quam laborum" . 所以我应该检查apiİf第一个数据id =1然后我应该发布"id labore ex et quam laborum" I'm open with any advice. 我有任何建议。 Thank you 谢谢

You could use the rxjs switchMap operator. 您可以使用rxjs switchMap运算符。

Just import it: 只需导入它:

import 'rxjs/add/operator/switchMap';

And then use it like this: 然后像这样使用它:

this.http.get('/api/items').switchMap(data => {
  // do something with your data
  return this.http.get(`https://api.com/${data.id}`); // use it for example in your http request
}).subscribe(data => {
    this.results = data['results'];
});

You can find more about it here . 你可以在这里找到更多相关信息。

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

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