關注此課程https://www.pluralsight.com/courses/angular-2-getting-started和github材料product.service該課程嘗試 避免每次點擊鏈接時調用http.get()請求。 我認爲每次加載文件而不是將其作爲對象保存在內存中是一件很大的浪費。如何緩存並共享http get()響應?
試圖將這段代碼:
getProducts(): Observable<IProduct[]> {
return this._http.get(this._productUrl)
.map((response: Response) => <IProduct[]> response.json())
.do(data => console.log('All: ' + JSON.stringify(data)))
.catch(this.handleError);
}
這一個:如果this._observable
是underfined this._observable=this._http.get(this._productUrl)
但它被稱爲
public _observable: Observable<IProduct[]>;
getProducts(): Observable<IProduct[]> {
console.log('_observable before: ' + (this._observable));
if(this._observable===undefined){
console.log('_observable inside 1: ' + (this._observable));
this._observable=this._http.get(this._productUrl)
.map((response: Response) => <IProduct[]> response.json())
.do(data => console.log('All inside observable: ' + JSON.stringify(data)))
.catch(this.handleError);
console.log('_observable inside 2: ' + (this._observable));
}
console.log('_observable after: ' + (this._observable));
return this._observable;
}
這行不應該叫做! !
在Chrome控制檯:
_observable before: [object Object]
product.service.ts:25 _observable after: [object Object]
product.service.ts:20 All inside observable:...
最後行不應該出現!
是對ProductListComponent訂閱可觀察一遍嗎?如果是這樣,它會再次執行它。 – DeborahK
查看這裏介紹的解決方案:http://stackoverflow.com/questions/36271899/what-is-the-correct-way-to-share-the-result-of-an-angular-2-http-network-調用 – DeborahK
實際上,您引導我朝着正確的方向 它是關於緩存和添加魔法字publishReplay(1)和refCount http://www.syntaxsuccess.com/viewarticle/caching-with-rxjs-observables-in -angular-2.0 如果您發佈答案,我會將其標記爲答案 – Ivanesses