我的角度应用程序中存在这个问题,因为我正在缓存数据。在我使用onCreate()函数添加内容之后。我在successl 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);
});
}
答案 0 :(得分:0)
对.publishReplay().refCount()
you can read it here.进行了一些研究我认为问题在于您在订阅之前使用.publishReplay().refCount()
,因此主题在一次迭代后被标记为已完成,因此它将会再也不会开火Read about it here.我自己并不熟悉,但值得一试。
如果不是那样尝试删除publishReplay的参数并尝试手动取消订阅以查看它是否有任何结果。