如何不使用angular httpClient.subscribe()缓存http响应



我的应用程序使用的是离子/角度。在启动屏幕(而不是splashscreen(中,我从带有的服务器中获得了一个.json文件

this.apiService.getEvents().subscribe res => {
console.log(res);
});

getEvents(): Observable<any> {
let headers = new HttpHeaders({
'Content-Type': 'application/json; charset=utf-8'
});
return this.httpClient.get("https:www.someURL.com/somejson.json", { headers: headers });
}

但是Observable正在缓存整个响应。我们的文件";somejson.json"是手动更改的,ionic应用程序应在每次appstart时重新加载其内容。

那么,如何不缓存HTTPGET呢?提前感谢;(

请在订阅前使用destroy以避免缓存。

private readonly _destroy$: Subject<void> = new Subject<void>();
this.apiService.getEvents().pipe(takeUntil(this._destroy$)).subscribe res => {
console.log(res);
});

请求不是由Angular缓存的,而是由浏览器缓存的。您可以通过每次生成唯一的URL来避免缓存

getEvents(): Observable<any> {
let headers = new HttpHeaders({
'Content-Type': 'application/json; charset=utf-8'
});
return this.httpClient.get(`https:www.someURL.com/somejson.json?q=${Date.now()}`, { 
headers: headers
});
}

最新更新