首页 > 解决方案 > 页面内容仅在硬刷新后显示,而不在重定向时显示

问题描述

如何在不进行硬刷新的情况下重定向到另一条路线?

我第一次创建了一个离子/角度应用程序。这是一项调查,在用户回答了一组问题后,他们将被重定向到结果页面。

我重定向到结果页面如下所示:

import { Router } from '@angular/router'
private router: Router
this.router.navigate(['/result/' + this.current_survey.id]);

我只在硬刷新后看到我的结果,我不知道为什么。

发生重定向的代码在我的调查组件中,我重定向结果组件。在 ngOinit() auf 结果组件中,我从 Web 数据库中检索保存的数据,但此时数据不存在。

调查组件中的代码块如下所示:

if (this.current_survey.finished) {
        this.calculateResult().then((result) => {
          this.current_survey.result = result;
          this.storageService.update(this.current_survey, this.helperService.SURVEY_STORAGE).then(
            (value) => {
            this.storageService.removeKey(this.helperService.LAST_NOT_FINISHED_SURVEY);
            this.router.navigate(['result', this.current_survey.id]);
          });
        });
    }

我的结果组件中的 ngOinit 看起来:

// Check if survey is a new survey or should be load from previos surveys
    let survey_id;

    // Get id from route, if any id was given in url
    this.route.paramMap.subscribe((params: ParamMap) => {
      survey_id = params.get('id');
    });

    if (survey_id !== null) {
      survey_id = parseInt(survey_id, 10);
    }

    this.showResults(survey_id);

在我的结果组件中的 ngOinit 中调用的方法如下所示:

 const key = this.helperService.SURVEY_STORAGE + '_' + survey_id;
      // Preload all neccessary data
      this.storageService.getDataByName(key).then((data) => {
        if (data.length === 0) {
            this.alertService.show('Error', '', 'Result not found :(');
        } else {
          this.survey = data.shift();
        }
      });

标签: javascriptangularionic-framework

解决方案


也许您可以提供更多信息。调查结果存储在哪里?如果它们存储在数据库中,则在结果页面组件的 ngOnInit() 方法中,您应该能够查询最新结果。另一种选择可能是,当您导航到结果页面时,您已经在调查结果完成发布之前完成了此操作。在尝试检索值之前,您可以执行以下操作以确保它已保存:

save(surveyResults, surveyId) {
    this.someService.postResults(surveyResults)
    .subscribe(() => this.navigate(['result', surveyId]))
}

推荐阅读