Angular2 : The responses to HTTP get requests are being cached in IE

冷暖自知 提交于 2019-12-24 08:29:18

问题


The responses to most HTTP get requests are being cached in IE Edge. It doesn't happen in other browsers as far as I know. I fixed that on older versions, but How can I do it on Angular2?

any help would be appreciated.


回答1:


A common fix is to add a random query parameter value to the request URL. You can create a custom Http implementation that does that for each request.

@Injectable() 
class NoCacheHttp extends Http {
  constructor(_backend: ConnectionBackend, _defaultOptions: RequestOptions) {
    super(_backend, _defaultOptions);
  }

  get(url: string, options?: RequestOptionsArgs) : Observable<Response> {
    let newUrl = /* add some random or incrementing number query parameter to URL */
    return super.get(newUrl, options);
  }

  post(...)
  ...
}
@NgModule({
  imports: [HttpModule],
  export: [HttpModule],
  providers: [{provide: Http, useClass: NoCacheHttp}]
})
export class NoCacheHttpModule {}
@NgModule({
  imports: [BrowserModule, NoCacheHttpModule],
  declarations: [AppModule],
  bootstrap: [AppModule]
})
export class AppModule {}


来源:https://stackoverflow.com/questions/40988263/angular2-the-responses-to-http-get-requests-are-being-cached-in-ie

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!