How to reuse angular observable across multiple components? (To avoid having to duplicate a request twice?)

后端 未结 3 1972
心在旅途
心在旅途 2021-01-06 01:00

I have a service \"MyHttpService\" that includes an observable like this:

grabData() {
return this.http.get(\'myaddress\')
                         .map((res         


        
3条回答
  •  一向
    一向 (楼主)
    2021-01-06 01:41

    When you subscribe in TwoComponent it will cause the http request to be sent again. The best way for you to prevent multiple http calls would be to store the response data in your service and before the http call, check if you have already saved the data and if so just directly returning the data.

提交回复
热议问题