1

I have a component which uses a service that makes an HTTP call:

getTrendingTerms(): Observable<Object[]> {
     return this.http.get(this.trendsUrl)
       .map(this.mapResponse)
       .catch(this.handleError);
}

private mapResponse(response: Response) {
   let body = response.json();
   return body || {};
}

and it works just fine.

However, I'm now introducing a new (unrelated to the first) component that will need to use that same data. Obviously I don't want to make a second HTTP call again, so what's the best strategy here? I'm very new to Angular 2 (and Observables) so this may be far simpler than I'm expecting ..

user1381745
  • 3,850
  • 2
  • 21
  • 35
  • if you're using data in more than one component, consider putting it in a service and then accessing it from there for both components. For the service: Fetch and cache the data. then when you query the service from your components, return the cached data if it exists, otherwise wait for the data to be returned and then return it. Consider having your service function return an observable. – dafyddPrys Sep 02 '16 at 11:03
  • I think that I made a mistake, as the answers on that quetion are outdated, if it doesn't solve your problem please comment here with your `angular2 version` to reopen your question :) – Ankit Singh Sep 02 '16 at 11:19
  • [this is fairly new](http://stackoverflow.com/questions/35878160/angular2-how-to-share-data-change-between-components) see if it helps – Ankit Singh Sep 02 '16 at 11:22

0 Answers0