Ionic 如何不使用Angular httpClient.subcribe()缓存http响应

tzdcorbm  于 2022-12-08  发布在  Ionic
关注(0)|答案(2)|浏览(136)

我正在为我的应用程序使用ionic/angular。在启动屏幕(不是闪屏)中,我从服务器获得了一个.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”是手动更改的,离子应用程序应该在每次appstart时重新加载它的内容。
那么,如何不缓存HTTPGET呢?)

mspsb9vt

mspsb9vt1#

请求不是由Angular缓存的,而是由浏览器缓存的。

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
    });
}
3okqufwl

3okqufwl2#

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

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

相关问题