この回答はRxJS 5を指し、応答が受信された後に完了してからのrest-callの典型的なものです)、publishLast
:
this.http
.get()
.map()
.publishLast()
.refCount();
そして、(あなたの最も柔軟性を提供します)第三の方法は、外部BehaviorSubject
またはReplaySubject
を使用することです:
public myData$: BehaviorSubject<any> = new BehaviorSubject(null); // initial value is "null"
public requestData(): BehaviorSubject<any> {
this.http
.get()
.map()
.do(data => this.myData$.next(data))
.subscribe();
return this.myData$.skip(1); // the returned subject skips 1, because this would be the current value - of course the skip is optional and depends on the implementation of the requesting component
}
あなたの成分(複数可)のことができます現在 "キャッシュされている"データを取得する場合はmyData$.subscribe(...)
、最新のデータを取得する場合はrequestData().subscribe(...)
を介してデータを取得してください。