简体   繁体   English

Angular 应用程序在将一个视图导航到另一个视图时冻结,并且具有 HTTP 请求的高负载响应

[英]Angular App gets freeze when navigating one view to another with high payload response of HTTP Request

I have an angular 9.1.11 application that is gets freezed after navigating from navigating from one page to another page (different module with lazy loading).我有一个 angular 9.1.11 应用程序在从一个页面导航到另一个页面后被冻结(具有延迟加载的不同模块)。

My scenario is like:我的场景是这样的:

  • I have an action button View Report that navigates to different route say /reportData with query params appended /reportData?reportId=XYZ&reportDate=YZX .我有一个操作按钮View Report ,它导航到不同的路线,例如/reportData并附加了查询参数/reportData?reportId=XYZ&reportDate=YZX

  • In my ReportComponent, I'm taking the queryParams from ActivatedRoute and using the params I'm calling a HTTP request在我的 ReportComponent 中,我从ActivatedRoute获取 queryParams 并使用我正在调用 HTTP 请求的参数

  • the Request will respond with heavy payload with 4-5 mb minimum as response.请求将以4-5 mb的最小负载响应作为响应。

When I click the View Report the page freeze, after few seconds it redirecting to /reportData?reportId=XYZ&reportDate=YZX and after that HTTP call gets hit.当我单击View Report时,页面冻结,几秒钟后它重定向到/reportData?reportId=XYZ&reportDate=YZX ,然后 HTTP 调用被击中。

Additional information:附加信息:

  • I added timer to track how log and where the freeze take place using Console.time()我添加了计时器来使用Console.time()来跟踪日志和冻结发生的位置
viewReport: 0.004150390625ms Start                                               --> from page One
viewReport: 186.335205078125ms Report - OnInit                                   --> Freezed
viewReport: 17733.464111328125ms Report - After ViewInit
viewReport: 17754.35009765625ms - viewReport fetchHttp Inital                    --> HttpRequest with upto 4-5 mb response
viewReport: 23164.468994140625ms Report - viewReport fetchHttp subscribed
viewReport: 23188.69091796875ms Report - fetchHttp subscribed End
viewReport: 23188.951904296875ms End

The component that presents the problem is the following:出现问题的组件如下:

Page One:第一页:

viewReport(reportForm: FormArray | FormGroup) {
    console.time("viewReport");
    console.timeLog("viewReport", "Start");
    const queryParams: Params = {
        reportId: reportForm.get("reportUuid").value,
        reportDate: reportForm.get("reportDate").value
    };

    this.router.navigate(["/reportData"], {
        queryParams,
        queryParamsHandling: "merge" // remove to replace all query params by provided
    });
}

Report Component:报告组件:

@Component({
selector: "kt-report-view",
templateUrl: "./report-view.component.html",
styleUrls: ["./report-view.component.scss"],
changeDetection: ChangeDetectionStrategy.OnPush,
providers: [ReportService]
})
export class ReportViewComponent implements OnInit, OnDestroy, AfterViewInit {

reportData: string;

reportLoader$: BehaviorSubject<boolean> = new BehaviorSubject(false);

private reportId: string;
private reportDate: any

constructor(
    private router: Router,
    private reportService: ReportService,
    private activatedRouter: ActivatedRoute
) {}

ngOnInit() {
    console.timeLog("viewReport", "Report - OnInit");          // Freezes Here
    const bodyName = document.getElementsByTagName("body")[0];
    bodyName.classList.add("bg-white");
}

ngAfterViewInit(): void {
    console.timeLog("viewReport", "Report - After ViewInit");
    this.activatedRouter.queryParams.subscribe(
        (res: { reportId: string; reportDate: any }) => {
            if (res) {
                this.reportId = res.reportId;
                this.reportDate = res.reportDate;
                this.fetchReportData();                        // calling HTTP Request
            }
        }
    );
}

ngOnDestroy(): void {
    const bodyName = document.getElementsByTagName("body")[0];
    if (bodyName.classList.contains("bg-white")) {
        bodyName.classList.remove("bg-white");
    }
}

fetchReportData() {
    console.timeLog("viewReport", "Report - viewReport fetchHttp Inital");
    this.reportLoader$.next(true);
    this.reportService.getReportData(this.reportId, this.reportDate).subscribe(
        async (report: any) => {
            console.timeLog("viewReport", "Report - viewReport fetchHttp subscribed Inital");

            const data: any = await this.constructOverAllHtml(report);

            console.timeLog("viewReport", "Report - fetchHttp subscribed End");

            this.reportData = data;
            this.reportLoader$.next(false);

            console.timeEnd("viewReport");
        },
        (err) => {
            this.reportLoader$.next(false);
            console.log(err);
        }
    );
}

private constructOverAllHtml(report: any): Promise<any> {
    return new Promise((resolve, reject) => {
        let finalContent: string = "";
        const mainReportHTML = report.mainReport.reportHTML;

        finalContent = mainReportHTML;
        if (report.childReport)
            report.childReport.map((reportData: any, index: number) => {
                finalContent += ` <p>&nbsp;</p><p><strong>Child Report:
                 ${index + 1}</strong></p> ${reportData.reportHTML}`;
            });
        resolve(finalContent);
    });
}

} }

why are you dragging data with route params that too of such huge amount.您为什么要使用如此庞大的路由参数来拖动数据。 even if it's to a single route or taking between multiple routes, pass very fewer data with route params or query string,.即使是单条路线或在多条路线之间使用,使用路由参数或查询字符串传递的数据也非常少。 of say, simple string, small object or small array.例如,简单的字符串、小 object 或小数组。

My Suggestion would be我的建议是

  1. instead of passing your data in route try to save in local storage/ session storage this way you can fetch 4-5mb data easily.不要在路由中传递您的数据,而是尝试保存在本地存储/ session 存储中,这样您可以轻松获取 4-5mb 数据。
  2. data in your route will be present till your 'angular's session' lasts (as it being a single page application) once you refresh your page angular will lose its data as it stores the request temporally in the method being called once you refresh the method it loses the data.刷新页面后,您的路由中的数据将一直存在,直到您的“角度会话”持续(因为它是单页应用程序)丢失数据。 also, what will you do in case your route fails, your data will be lost!另外,如果您的路线失败,您会怎么做,您的数据将丢失!
  3. because you are storing all the data in local storage you can access data at any point of time during the application run you can also encode your data using atob() method for security purpose.因为您将所有数据存储在本地存储中,所以您可以在应用程序运行期间的任何时间点访问数据,您还可以使用 atob() 方法对数据进行编码以实现安全目的。 and also do not forget to delete the data once done.完成后不要忘记删除数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 RxJS 发出多个 HTTP 请求并将响应合并到一个有效负载中,然后将 map 合并到另一个动作中? - How to make multiple HTTP requests with RxJS and merge the response into one payload and map it to another action? 在Angular 4 http请求的Request Payload中,表单数据似乎是其他内容 - Form Data Appears to be something else in Request Payload in Angular 4 http request HTTP请求可与cURL一起使用,但在使用javascript的浏览器中进行时会获得CORS响应 - HTTP request works with cURL but gets CORS response when made in browser with javascript 调用$ http以响应另一个http-angular js - call $http in response of another http - angular js 当http请求授权失败时,Ionic 2从服务导航 - Ionic 2 navigating from service when http request authorization fails 在Angular 6中一对一处理HTTP响应 - Handling http response one by one in Angular 6 Angular JS $ http请求-缓存成功响应 - Angular JS $http request - caching success response Angular 2 http Observable请求未返回响应头 - Angular 2 http Observable request not returning response header 从 api 检索发布请求响应并将其传递给另一个发布请求以传递给 angular8 应用程序 - Retrieving the Post Request response from api and passing it on to another post request to be passed on to angular8 app 根据HTTP请求动态更新角度视图 - Update angular View based on http request dynamically
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM