Fetch data once with Observables in Angular 2
<p>I have a service, what is used several times from a lot of my Angular 2 components. It fetches customer data from a Web API and returns an Observable:</p>

<pre><code>getCustomers() {
   return this.http
        .get(this.baseURI + this.url)
        .map((r: Response) =&gt; {
            let a = r.json() as Customer[];                       
            return a;                         
        });               
}    
</code></pre>

<p>I inject this service in my root component, and in every component that wants to access the customers I just subscribe to that Observable:</p>

<pre><code>this.customerService.getCustomers().subscribe(v =&gt; this.items = v);
</code></pre>

<p>However, every component who subscribes to my Observable causes another execution of the HTTP-request. But to fetch the data only once is enough. 
If I try share(), it does not solve my problem:</p>

<pre><code>getCustomers() {
   return this.http
        .get(this.baseURI + this.url)
        .map((r: Response) =&gt; {
            let a = r.json() as Customer[];                       
            return a;                         
        }).share();               
}   
</code></pre>

<p>Still the same issue. Any proposals which operators I have to use to only fetch data once?</p>