Multiple $http requests in Ionic2

我只是一个虾纸丫 提交于 2020-02-05 05:14:47

问题


I want to know whether the Multiple request :

if my $http request 1 start , let's say $http request 1 ends and tried to call $http request 2. my question how can i create a multiple request ?

for example : call $http request 1 then $http request 2.


回答1:


As I understand, you're trying to make more than one http request and then process the response when all those request have ended. For instance, you may need to load data from more than one source, and delay the post-loading logic until all the data has loaded.

If that's the case, you could use ReactiveX Observables because it provides a method called forkJoin() to wrap multiple Observables.

import {Injectable} from '@angular/core';
import {Http, Response} from '@angular/http';
import {Observable} from 'rxjs/Rx';

@Injectable()
export class MultipleHttpService {

  constructor(private http:Http) { }

  // If any single request fails, the entire operation will result in an error state.
  getData0AndData1() {
    return Observable.forkJoin(
      this.http.get('/app/data0.json').map((res:Response) => res.json()),
      this.http.get('/app/data1.json').map((res:Response) => res.json())
    );
  }

}

Then you could get all the data by subscribing to that observable:

// Code in your page ...
this.myMultipleHttpService.getData0AndData1()
    .subscribe(
      data => {
        this.data0= data[0]
        this.data1 = data[1]
      },
      err => console.error(err)
    );


来源:https://stackoverflow.com/questions/38842832/multiple-http-requests-in-ionic2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!