我做了一个角网站,其中包含一个“添加”和“删除”的形式来处理数据对表在同一页上。角不更新缓存页面启用
当我测试它在本地或Chrome浏览器开发控制台(禁用缓存),当我添加一个新的项目或删除一个,该表将自动刷新本身。当我在客户端的生产服务器(IIS服务器)上对其进行测试时,它仅在打开Chrome开发人员控制台的情况下起作用。否则,他们必须使用CTRL + F5刷新缓存并在页面上显示更改。
这是组件的代码:
addProduct() {
this._productsService.addProduct(this.addFormProductItem)
.subscribe(v => {
this._productsService.getProducts().subscribe(
products => {this.products = products, this.onChangeTable(this.config)}
);
});
this.addmodalWindow.hide();
return;
}
onChangeTable(config: any, page: any = {
page: this.page,
itemsPerPage: this.itemsPerPage
}): any {
if (config.filtering) {
Object.assign(this.config.filtering, config.filtering);
}
if (config.sorting) {
Object.assign(this.config.sorting, config.sorting);
}
this.ng2TableData = this.products;
this.length = this.ng2TableData.length;
let filteredData = this.changeFilter(this.ng2TableData, this.config);
let sortedData = this.changeSort(filteredData, this.config);
this.rows = page && config.paging ? this.changePage(page, sortedData) : sortedData;
this.length = sortedData.length;
}
我的猜测是,它要么是相关的一些服务器配置或给的WebPack代码。然而,我对后者并不熟悉,我只是把它留在了我使用的起始包上。
我创建a gist with the webpack,因为它是一个长文件
编辑1:一些额外的研究之后,我试着添加以下的web.config文件上的应用程序的根文件夹。
<caching enabled="false" enableKernelCache="false">
<profiles>
<add extension=".css" policy="DontCache" kernelCachePolicy="DontCache" />
<add extension=".html" policy="DontCache" kernelCachePolicy="DontCache" />
<add extension=".js" policy="DontCache" kernelCachePolicy="DontCache" />
</profiles>
</caching>
但是,我仍然有同样的行为。添加开发控制台关闭的项目,它不会更新表格。但是,如果我已经打开了dev控制台并禁用了缓存,那么它会在不需要刷新的情况下更新它。
编辑2:在隐身窗口上工作并不能解决问题。
编辑3:在index.html上添加元标记并不能解决问题。
<meta http-equiv="Cache-control" content="no-cache, no-store, must-revalidate">
<meta http-equiv="Pragma" content="no-cache">
编辑4:
getProducts() {
return this._http.get(this.API + '/products/all')
.map((response: Response) => <Product[]>response.json().products);
}
addProduct(product:Product) {
if (this._loggedInGuard.isLoggedIn()) {
let token = localStorage.getItem('my.token');
let body = JSON.stringify(product);
let headers = new Headers(
{ 'Content-Type': 'application/json',
'Authorization': 'Bearer ' + token});
return this._http
.post(this.API + "/products/store", body, {headers: headers})
.map(res => res.json())
.catch(this._responseService.catchBadResponse)
.do(() => this._responseService.success('Success. You added a product!'));
}
}
编辑5
唯一的解决办法我能找到的是我每次做一个更新的数据与location.reload(true)
时间来重新加载窗口。但是,再次,这只适用于Firefox而不适用于Chrome。我不会接受你必须放弃使用单页应用程序来实现这一功能的唯一原因。
你可以从你的服务中显示你的'getProducts()'和'addProduct()'。另外,当你在组件中调用'addProduct'两次会发生什么? –
更新了问题......无论多少次添加产品都无关紧要。该表保持不变,除非我打开开发控制台或清除缓存并刷新。 – Tasos
这两个请求都被执行? –