在Angular中,可以使用http请求的cache
选项来控制是否从缓存中获取数据。
以下是一个示例代码,展示如何在IE 11中使用Angular进行GET请求并从缓存中获取数据:
import { Component, OnInit } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
@Component({
selector: 'app-example',
template: `
{{ item }}
`,
})
export class ExampleComponent implements OnInit {
data: any[];
constructor(private http: HttpClient) {}
ngOnInit() {}
getData() {
const url = 'https://api.example.com/data'; // 替换为实际的API URL
const headers = new HttpHeaders()
.set('Cache-Control', 'no-cache') // 禁用缓存
.set('Pragma', 'no-cache')
.set('Expires', 'Sat, 01 Jan 2000 00:00:00 GMT');
this.http
.get(url, { headers, responseType: 'json', withCredentials: true })
.subscribe((response) => {
this.data = response;
});
}
}
在上述代码中,我们通过设置请求头中的Cache-Control
,Pragma
和Expires
来禁用缓存。这样,即使在IE 11中,GET请求也不会从缓存中获取数据。