我通過網格頁面更改上的 api 調用從服務器調用數據。調用此方法時,我會動態獲取所有列的表模式,并在角度方面使用它進行其他工作。問題是我只需要獲取此表數據一次。如何將數據保存在內存中,以便如果用戶在客戶端更改頁面,我不必再次訪問數據庫來獲取模式數據? [Route("api/MyRoute/GetData/{Skip}/{Take}")] [HttpGet] [AllowAnonymous] public HttpResponseMessage GetData(string Skip, string Take) { try { if (Result == null) { Result = DynamicSqlQuery(Convert.ToInt32(Skip), Convert.ToInt32(Take)); }}
1 回答

森林海
TA貢獻2011條經驗 獲得超2個贊
如果您不希望將數據保存在角度方面,您可以在服務中進行 http 調用,例如
dataStructure = [];
getDataStructure() {
if (this.dataStructure) {
return this.dataStructure;
}else{
return this.http.get<any>(
this.url + 'values',
{ headers: headers }
).pipe(
tap( data => this.cachedData = data )
);
}
}
點擊不會訂閱,您在組件中正常訂閱,當數據到達時,點擊將副本保存在內存中,因此下一次調用會返回該副本。
請參閱 SB 中的示例(選項 12):https ://stackblitz.com/edit/angular-rb5vmu
您可以在日志中看到第一個調用是從服務器獲取的,然后是從緩存中獲取的。
- 1 回答
- 0 關注
- 170 瀏覽
添加回答
舉報
0/150
提交
取消