I have an Angular 5 application with the following code in a component:
ngOnInit() {
Observable.forkJoin([
this.highlightedInsight = this.insightService.getHighlightedInsight(),
this.insights = this.insightService.getInsightsList(),
this.topics = this.insightService.getInsightTopicsList()
]).subscribe(
response => {},
error => {
console.log('An error occurred:', error);
},
() => {
this.loading = false;
});
}
My html:
<div class="internal-wrapper" *ngIf="!loading">
<app-highlighted-insight [insight]="highlightedInsight | async"></app-highlighted-insight>
<app-topic-filter [topics]="topics | async"></app-topic-filter>
<app-insight-list [insights]="insights | async"></app-insight-list>
</div>
In my Chrome network tab, each of the 3 API calls is running twice. What am I doing wrong?
Both the
async
pipe and theforkJoin.subscribe
are creating separate network requests.Use Observable.share to prevent resubscribing
But because the results aren't needed until the end (when
!loading
), it can be simplified to this:html: