页面内容仅在硬刷新后显示,而不在重定向时显示

时间:2019-03-22 20:04:48

标签: javascript angular ionic-framework

如何在不进行强制刷新的情况下重定向到其他路由?

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

我重定向到结果页面的样子如下:

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();
        }
      });

1 个答案:

答案 0 :(得分:1)

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

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