我在我的角度应用程序中遇到了这个问题,因为我正在缓存数据。在我使用 onCreate() 函数添加内容之后。我在成功订阅函数中调用了 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);
});
}