【发布时间】:2019-08-13 21:08:26
【问题描述】:
如何在不进行硬刷新的情况下重定向到另一条路线?
我第一次创建了一个离子/角度应用程序。这是一项调查,在用户回答了一组问题后,他们将被重定向到结果页面。
我的重定向到结果页面如下所示:
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();
}
});
【问题讨论】:
标签: javascript angular ionic-framework