如何防止或避免缓存角2个+应用程序?

问题描述 投票:1回答:2

我在那里我送参数,这反过来又产生动态的PDF格式和API向我发送新生成的文件的路径的API的情况。当我在浏览器中打开文件时,它显示新生成的文件,但在我的DOM我仍然看到旧的文件,直到&除非我关闭浏览器和再次击中了API。我预览生成的PDF文件,如下所示:

TS部分:

this.http.postMethod("report/auditAdmin", data).subscribe((res: any) => {
        this.toolbar = "#toolbar=0&navpanes=0";
        this.pdfSrc = res.filepath + this.toolbar;
        this.Url = this.sanitizer.bypassSecurityTrustResourceUrl(this.pdfSrc);
      });

HTML部分:

<object [data]="Url" type="application/pdf" width="100%" height="1000px"></object>
angular pdf angular6 browser-cache cache-control
2个回答
1
投票

这个问题是不是与API缓存或角,该问题是与目标控制。当我们尝试加载像pdf文件或任何其他外部内容对象,在数据发送GET请求来显示内容(get请求可以在浏览器的网络选项卡中viewd)。

因此,简单的办法就是追加查询字符串到PDF路径。

this.http.postMethod("report/auditAdmin", data).subscribe((res: any) => {
            this.toolbar = "#toolbar=0&navpanes=0";
            let ramdom = Math.random();
            this.pdfSrc = res.filepath + "?v=" + random + this.toolbar;
            this.Url = this.sanitizer.bypassSecurityTrustResourceUrl(this.pdfSrc);
          });

现在,只要你的对象尝试做一个GET请求,PDF文件的每个请求将新鲜,因为查询字符串,因此数据将不会从缓存中加载。


0
投票

为了防止/避免角2+版本中,你可以通过重写RequestOptions实现缓存。

第1步:创建自定义RequestOptions。

import { Injectable } from '@angular/core';
import { BaseRequestOptions, Headers } from '@angular/http';
@Injectable()
export class CustomRequestOptions extends BaseRequestOptions {
    headers = new Headers({
        'Cache-Control': 'no-cache',
        'Pragma': 'no-cache',
        'Expires': 'Sat, 01 Jan 2000 00:00:00 GMT'
    });
}

步骤2在根AppModule.ts提供

@NgModule({
    ...
    providers: [
        ...
        { provide: RequestOptions, useClass: CustomRequestOptions }
    ]
})

希望上述解决方案将解决您的问题!

© www.soinside.com 2019 - 2024. All rights reserved.