简体   繁体   中英

Angular how to handle multiple HTTP calls

I have two HTTP sequence call as below :

  1. Reading data from JSON file : which gives me a "URL" from JSON file

  2. GET/POST API Call : using URL from 1.

I used below code :

 this.httpClient.get('/api/apiserver.json').subscribe( apipath => { if (apipath['server'] !== undefined) { this.httpClient.get(apipath['server'] + '/api/manual/xyz?parameter1=' + parameterid).subscribe( (data: any[]) => { // Binding and showing user data } ) } });

I don't want to call API 1 every time ie Can I write HTTP logic somewhere and use it globally across project and then sequentially in component for other GET/POST API call. Please suggest.

Welcome to SO. You can create a new service and call the API1 in there in a method like this -

getMethod() {
  return this.httpClient.get('/api/apiserver.json')
}

Then, you can subscribe to this method whenever/wherever you need to. Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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