0
我在我的角度應用程序中有這個問題,因爲我正在緩存數據。使用onCreate()函數添加內容後。我在successfull subscribe函數中調用getAll()函數以獲取新數據。但是,我仍然沒有得到新的數據。我相信這是因爲我服務中的緩存數據。我如何知道是否有新數據或者當有新數據時如何修復我的現有數據和刷新數據?在角度緩存和刷新數據
服務
getAll() {
if(!this.materials) {
this.materials = this.httpClient.get<any>(this.url)
.map((response => response))
.publishReplay(1)
.refCount();
}
return this.materials;
}
GETALL()TS
getAllMaterials() {
this.subscription = this.materialsService.getAll()
.subscribe(
(data:any) => {
this.materials = data.materials;
console.log(data);
},
error => {
alert("Error");
console.log(error);
});
}
的onCreate()-ts
onCreateMaterial(form: NgForm){
const formData = {
sku: form.value.sku,
name: form.value.name,
supplier_id: form.value.supplier,
price: form.value.price
}
this.materialsService.addMaterial(formData)
.subscribe(
data => {
let message = data.message;
alert(message);
console.log(data);
this.modalRef.close();
this.getAllMaterials();
},
error => {
alert("Error Adding");
console.log(error);
});
}