Is it possible to cache the HTTP requests in the Angular 10 version
public getPart(id: any): Observable { return this.http.get(`${this.baseUrl}${id}`) .pipe(map((res: any) => res.data)); }
Yes it was possible then and now also in the latest version using rxjs/operators
rxjs/operators
... private records$: Observables<any>; ... public getPart(id: any): Observable<any> { if(!this.records$) { this.records$ = this.http .get(`${this.baseUrl}${id}`) .pipe(map((res: any) => res.data), shareReplay(1)); } return this.records$ // reply the last result for all new subscribers }
Just pipe the stream through shareReplay operator. new subscriptions will NOT execute other HTTP requests but a cached value will be used/returned.
shareReplay