我正在编写一个 Angular SPA 应用程序,它使用 HttpClient 从我的后端获取值。
告诉它不要缓存的简单方法是什么?我第一次询问它获取值,然后它拒绝进行后续查询。
谢谢, 格里
<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">
或者,
在
headers
请求中添加 http
为:-
headers = new Headers({
'Cache-Control': 'no-cache, no-store, must-revalidate, post-
check=0, pre-check=0',
'Pragma': 'no-cache',
'Expires': '0'
});
HTTPInterceptors 是修改应用程序中发生的 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 { }
如何在网址中添加盐:
const salt = (new Date()).getTime();
return this.httpClient.get(`${url}?${salt}`, { responseType: 'text' });
同样的概念也用于 html(css 或 js)中的静态资源链接来欺骗缓存。在 url 中添加动态盐会导致每次重新加载目标,因为每次 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 }]
您还可以将唯一的查询参数附加到每个请求的 URL 中。这使得每个请求对于浏览器来说都是不同的,从而阻止它使用缓存的响应。
const uniqueParam = new Date().getTime();
const url = `your-api-endpoint?cacheBuster=${uniqueParam}`;
this.http.get(url).subscribe(data => {
// Handle the response data
});