I have a service that is used several times from a large number of my Angular 2 components. It retrieves client data from the web API and returns Observable:
getCustomers() {
return this.http
.get(this.baseURI + this.url)
.map((r: Response) => {
let a = r.json() as Customer[];
return a;
});
}
I am embedding this service in my root component, and in every component that wants to access clients, I just subscribed to this Observable:
this.customerService.getCustomers().subscribe(v => this.items = v);
However, every component that subscribes to my Observable causes a different HTTP request execution. But to receive data, only once is enough. If I try share (), this will not solve my problem:
getCustomers() {
return this.http
.get(this.baseURI + this.url)
.map((r: Response) => {
let a = r.json() as Customer[];
return a;
}).share();
}
Still the same problem. Any suggestions which operators should I use to retrieve data only once?