I have two components using the same service. On FirstComponent
, I get some data from a service:
getLiveData() {
Observable.interval(1000)
.mergeMap(() => this.service.getData())
.subscribe(
res => this.data = res,
err => this.error = err
);
}
At first, I've added another getLiveData()
to the SecondComponent
. However, that way I'm making two REST calls every second. That shouldn't be needed as I want to use the same service.
So, can I reuse that in the SecondComponent
without making two calls every time?
I tried to add the FirstComponent
as a service and called this.service.data
but I'm getting undefined
.
PS. I also need the values to be updated every second on both components.
UPDATE: I've posted a Plunker following Madhu's advice. I managed to get only one call per time, but I'm not getting any data from it.