Angular2 : The responses to HTTP get requests are

2019-08-03 19:01发布

问题:

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 {}