Angular 2 observable subscribing twice executes call twice

You may use the share operator on your result from HttpClient.get, like this:

var observable = this.httpClient.get('api/version/secured', { headers: httpHeaders })
  .pipe(share());

You’ll need to add the following import on top of your script:

import { share } from 'rxjs/operators';

The share operator makes an observable hot, i.e. shared between subscribers. But there’s a lot more to it, I would suggest this article to dive deeper (you can of course also google up hot vs cold observables to find more).

Leave a Comment

tech