簡體   English   中英

角度可觀察的緩存數據

[英]Angular observable caching data

我有一個基於Angular 6的angular項目,我的API是.net core 2.1。

我已經實現了分頁。 使用郵遞員在.net核心 API中調用分頁方法效果很好。 從有角度的一端,我使用可觀察的。 ngOnInit事件上,我調用fetchRecords()函數,並顯示前10條記錄。 當我單擊下一步時,我再次調用fetchRecords()方法,並傳遞正確的分頁參數,但是該頁面仍保留前10條記錄,在console.log()中,我還注意到來自API的記錄仍然相同前10條記錄。 我有一個刷新按鈕,它再次調用fetchRecords()方法,當我立即單擊刷新按鈕時,沒有任何變化。 但是,如果我等待約7+秒,則會顯示接下來的10條記錄。 在我的可觀察范圍內是否進行了某種形式的緩存,可以使請求響應保持幾秒鍾? 如果是這樣,我該如何糾正?

/* My Fetch Requests method in my component.ts */

fetchRecords(){

let requestData:any = {
   skip: (this.page * this.rowsPerPage) - this.rowsPerPage,
   take: this.rowsPerPage
}


this.api.post('apiUrl/fetchRecords', requestData).subscribe(res=>{ // this is where i subscribe
 console.log('results', res); // Does not change unless delayed for 10 seconds
}
error=>{

})

}



goToNextPage(){
  this.page = this.page + 1;
    this.fetchRecords(); 
}

goToPreviousPage(){
  this.page = this.page - 1;
    this.fetchRecords();
}



/* Post Method */

post(url, obj) : Observable<any>{
    return this.http.post(`${this.baseUrl}/${url}`, obj, this.options)
    .map((response: Response) => {
        return response.json();
    });
}

我找到了解決方案。 顯然我只是從http請求中實現角度緩存響應。 我嘗試添加無緩存的Cache Control標頭,但所有其他方法均無效。 最終有效的方法是,將一個唯一的日期字符串作為參數添加到我的帖子網址中(成功地使每個請求網址都唯一),並且即使連續快速地加載,我的請求也可以正常運行。 這是一段代碼

post(url, obj) : Observable<any>{
    return this.http.post(`${this.baseUrl}/${url}`+ "?"+new Date().toString(), obj, this.options)
    .map((response: Response) => {
        return response.json();
    });
}

感謝大家的貢獻。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM