Which is the best way to manage subscribers on a Angular HTTP request in this case:
I have button that performs a get request and then prints the result:
Template
<button type="button" class="btn btn-primary" (click)="helloWorld()">Mod1</button>
Component
constructor(private heroService: HeroService) { }
helloWorld() {
this.heroService.getUser().subscribe(data => console.log(data));
}
Service
constructor(private _http: HttpClient) { }
getUser() {
return this._http.get("https://randomuser.me/api/");
}
The mainly problem here is, every time I press the button a new subscription is created, causing memory leaks and poor performance over time due to unused listeners.
How may I implement a multiple get request without affect the application performance?