It's hard to answer this reasonably well with out additional information here.
The first issue, the HttpClient should be in a service, not the component itself.
Then you can do something like:
//example a
ngOnInit() {
this.data = this.apiService.getData.pipe(
map(response => console.log(response)),
catchError(async error => console.error(error))
)
}
And then perhaps an async pipe on the template. This isn't the only choice,
ngOnInit(){
this.apiSerive.get().subscribe(s => {
this.data = s;
})
}
could also be entirely valid, as well with the operators you need.
Where memory leaks are likely to come in: If the component is often reused:
<div *ngFor="let chart of allAnalytics"><app-analytics [chart]="view"></app-analytics></div>
Potentially, each of those could have a stream that is just left, out there. Objects sitting in memory.
There are other potential issues too. Angular doesn't do great (yet) with a template calling a method. You might see additional calls for components being re-rendered as well as additional checks for updates, or updates creating more updates. Check this even just by logging everytime that method is called. You might see 2,4 or 20 messages logged when you expected 1.
If you went with example A instead of the original code, the mitigation strategy is pretty easy:
ngOnDestroy(){
this.myDataSubscription.unsubscribe();
}
This basically just makes sure each component has a reference to the same subscription, and any objects you needed are destroyed when the component leaves the DOM.