我正在编写一个使用 HttpClient 从后端获取值的 Angular SPA 应用程序。
如何简单地告诉它不要缓存?第一次请求时可以获取到值,但之后就拒绝了进行进一步的查询。
谢谢, Gerry
我正在编写一个使用 HttpClient 从后端获取值的 Angular SPA 应用程序。
如何简单地告诉它不要缓存?第一次请求时可以获取到值,但之后就拒绝了进行进一步的查询。
谢谢, Gerry
<meta http-equiv="cache-control" content="no-cache, must-revalidate, post-check=0, pre-check=0">
<meta http-equiv="expires" content="0">
<meta http-equiv="pragma" content="no-cache">
或者,在HTTP请求中添加标题:headers = new Headers({
'Cache-Control': 'no-cache, no-store, must-revalidate, post-
check=0, pre-check=0',
'Pragma': 'no-cache',
'Expires': '0'
});
HTTP拦截器是修改应用程序中发生的HTTP请求的好方法。它作为可注入服务而运行,可以在发生HttpRequest时调用。
HTTP拦截器:
import { Injectable } from '@angular/core';
import { HttpInterceptor, HttpRequest, HttpHandler, HttpHeaders } from '@angular/common/http';
@Injectable()
export class CacheInterceptor implements HttpInterceptor {
intercept(req: HttpRequest<any>, next: HttpHandler) {
const httpRequest = req.clone({
headers: new HttpHeaders({
'Cache-Control': 'no-cache',
'Pragma': 'no-cache',
'Expires': 'Sat, 01 Jan 2000 00:00:00 GMT'
})
});
return next.handle(httpRequest);
}
}
使用拦截器:
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { AppComponent } from './app.component';
import { HttpClientModule, HTTP_INTERCEPTORS } from '@angular/common/http';
import { CacheInterceptor } from './http-interceptors/cache-interceptor';
@NgModule({
imports: [ BrowserModule, FormsModule ],
declarations: [ AppComponent ],
bootstrap: [ AppComponent ],
providers: [
{ provide: HTTP_INTERCEPTORS, useClass: CacheInterceptor, multi: true }
]
})
export class AppModule { }
在url中添加盐如何?
const salt = (new Date()).getTime();
return this.httpClient.get(`${url}?${salt}`, { responseType: 'text' });
在HTML中,静态资源链接(如CSS或JS)也会使用相同的概念来欺骗缓存。通过向URL添加动态salt,每次目标都会重新加载,因为URL每次都不同,但实际上是相同的。
/static/some-file.css?{some-random-symbols}
我使用日期作为唯一数字的保证,而无需使用随机等方法。我们也可以为每个调用使用递增整数。
在我无法更改服务器配置的情况下,上述代码对我有效。
如Pramod所述,您可以使用http请求拦截器来修改或设置请求中的新标头。 对于较新版本的Angular(Angular 4+),以下是一种更简单的设置http请求拦截器标头的方法。该方法仅设置或更新某个请求标头。这样可以避免删除或覆盖一些重要的标头,例如授权标头。
// cache-interceptor.service.ts
import { Injectable } from '@angular/core';
import {
HttpInterceptor,
HttpRequest,
HttpHandler,
} from '@angular/common/http';
@Injectable()
export class CacheInterceptor implements HttpInterceptor {
intercept(req: HttpRequest<any>, next: HttpHandler) {
const httpRequest = req.clone({
headers: req.headers
.set('Cache-Control', 'no-cache')
.set('Pragma', 'no-cache')
.set('Expires', 'Sat, 01 Jan 2000 00:00:00 GMT')
})
return next.handle(httpRequest)
}
}
// app.module.ts
import { HttpClientModule, HTTP_INTERCEPTORS } from '@angular/common/http'
import { CacheInterceptor } from './cache-interceptor.service';
// on providers
providers: [{ provide: HTTP_INTERCEPTORS, useClass: CacheInterceptor, multi: true }]
const uniqueParam = new Date().getTime();
const url = `your-api-endpoint?cacheBuster=${uniqueParam}`;
this.http.get(url).subscribe(data => {
// Handle the response data
});