how to avoid multiple http requests when same component is used twice

kiran gudla

How to avoid multiple http requests when same component is used twice in template

https://stackblitz.com/edit/angular-8sgrgz

hello component is used twice in the template and its making calling service method for http data

  return this.http
    .get('https://api.openweathermap.org/data/2.5/weather? 
      q=London&appid=517c7bbf790248290ad52d57725c4d5f')
    .map((res) => {return res.json()})
                  .catch((error:any) => 
                       Observable.throw(error.json().error || 'Server 
     error')).share();} 

the http request should happen only once 
but its happening more than once
ritaj

Add the Behaviour subject to hold your data once you fetch it:

private data$: BehaviorSubject<any> = new BehaviorSubject(null);

Add a function to get data:

  getData() {
    return this.data$.pipe(
      switchMap(data => {
        if (data) {
          return of(data); // If data was saved, just return it (you're actually returning an observable of data, because switchMap needs observables to switch for every other observable emission)
        } else {
          return this.http.get("") // If it wasn't, make a new http request
            .pipe(tap(newData => this.data$.next(newData))); // And save the data on the subject
        }
      })
    );
  }

Hope that helps.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How to avoid same node.js packages installing several times when they are used as dependencies by different modules?

分類Dev

How to specific ip and hostname when making http or https requests?

分類Dev

Angular - Sequencing Multiple HTTP Requests

分類Dev

AngularJS form with multiple http requests

分類Dev

recv function fails when used twice

分類Dev

How to avoid calling observe method twice?

分類Dev

How to avoid using a var when doing multiple string replacements in a string

分類Dev

How to redirect all HTTPS requests to HTTP requests?

分類Dev

python- How to properly send http GET requests to different Host-IPs but same host

分類Dev

How to organize multiple Http Functions under same Function App

分類Dev

Multiple requests asynchronously with same callback function

分類Dev

Stop multiple AJAX requests in same page

分類Dev

Why "%I64d" is giving strange output when it is used multiple times in same format string?

分類Dev

RequireJS: onload making multiple http requests

分類Dev

Flutter Firestore: How to avoid conflicts in writing/updating a document that is available to multiple users at the same time?

分類Dev

How to Create Same resource Twice in Puppet

分類Dev

how to run awk twice across the same file

分類Dev

Alternative to HTTP to avoid corruption when downloading

分類Dev

How to avoid redirecting twice (react-router-redux)?

分類Dev

How to avoid Multiple Jbutton selections

分類Dev

How to avoid append multiple times

分類Dev

How to avoid @click on only one child component

分類Dev

How to convert CSS into LESS when there are multiple classes using same properties

分類Dev

How to check multiple object properties twice in JavaScript?

分類Dev

Multiple path names for a same component in Reach Router

分類Dev

How to avoid huge if-else checking when trying to find a value determined by multiple parameters?

分類Dev

Utilize same http session in javascript through many requests

分類Dev

When upgrading an rpm previously installed with prefixes, how to make sure the same prefixes are used?

分類Dev

How to avoid rails as_json creating flood of database requests

Related 関連記事

  1. 1

    How to avoid same node.js packages installing several times when they are used as dependencies by different modules?

  2. 2

    How to specific ip and hostname when making http or https requests?

  3. 3

    Angular - Sequencing Multiple HTTP Requests

  4. 4

    AngularJS form with multiple http requests

  5. 5

    recv function fails when used twice

  6. 6

    How to avoid calling observe method twice?

  7. 7

    How to avoid using a var when doing multiple string replacements in a string

  8. 8

    How to redirect all HTTPS requests to HTTP requests?

  9. 9

    python- How to properly send http GET requests to different Host-IPs but same host

  10. 10

    How to organize multiple Http Functions under same Function App

  11. 11

    Multiple requests asynchronously with same callback function

  12. 12

    Stop multiple AJAX requests in same page

  13. 13

    Why "%I64d" is giving strange output when it is used multiple times in same format string?

  14. 14

    RequireJS: onload making multiple http requests

  15. 15

    Flutter Firestore: How to avoid conflicts in writing/updating a document that is available to multiple users at the same time?

  16. 16

    How to Create Same resource Twice in Puppet

  17. 17

    how to run awk twice across the same file

  18. 18

    Alternative to HTTP to avoid corruption when downloading

  19. 19

    How to avoid redirecting twice (react-router-redux)?

  20. 20

    How to avoid Multiple Jbutton selections

  21. 21

    How to avoid append multiple times

  22. 22

    How to avoid @click on only one child component

  23. 23

    How to convert CSS into LESS when there are multiple classes using same properties

  24. 24

    How to check multiple object properties twice in JavaScript?

  25. 25

    Multiple path names for a same component in Reach Router

  26. 26

    How to avoid huge if-else checking when trying to find a value determined by multiple parameters?

  27. 27

    Utilize same http session in javascript through many requests

  28. 28

    When upgrading an rpm previously installed with prefixes, how to make sure the same prefixes are used?

  29. 29

    How to avoid rails as_json creating flood of database requests

ホットタグ

アーカイブ