I have 2 components UsersComponent and HelloComponent.
I am calling the same service with the same api calls on both the component.
But I want is that if UsersComponent is calling the api than HelloComponent should not call the api it should recieve the value from UsersComponent.
Currently network call is happening on both of the component.
Is there any way to do.Please help.
Stackblitz link:
This is called as cold observables. Whenever you subscribe to it, it'll make a new HTTP request.
Because every time you return a new
http.get
call.The solution here is to return the same pointer to that observable every time.
However, you can use rxjs
share
operator as well.Create service like this,
add it in providers list in app.module.ts
and inject it into both components constructor like,
and when any of the component gets loaded just check if
data
is null if not make api call and also updatedata
so that later service will not fetch it again.