cache observable code example
Example 1: angular observable cache
@Component({ ... })export class SomeOtherComponent implements OnInit { constructor(private cachedService: CachedService) { } ngOnInit() { this.cachedService.fetch(); this.cachedService.data$.subscribe(...); }}
Example 2: angular observable cache
import { Injectable } from '@angular/core';import { Http, Response } from '@angular/http';import { Observable, ReplaySubject } from 'rxjs';@Injectable()export class CachedService { data$: Observable<Response> = this.replaySubject.asObservable(); private replaySubject= new ReplaySubject<Response>(1); constructor(private http: Http) { } fetch() { this.http.get(...).subscribe(data => this.replaySubject.next(data)); }}