【发布时间】:2021-01-23 23:05:01
【问题描述】:
在我的 Ionic 5 应用程序中,我有以下导航路径。
PageHome -> PageA -> PageB
我已经为 PageA 实现了 CanDeactivate 保护。
export class LeavePageGuard implements CanDeactivate<isDeactivatable>{
canDeactivate(
component: isDeactivatable
): Observable<boolean> | Promise<boolean> | boolean {
return component.canPageLeave();
}
}
当用户在保存前编辑某些内容并按下返回按钮时,我会弹出一个弹出窗口来确认用户是否想离开。
async canPageLeave() {
if (this.byPassNav) {
this.byPassNav = false;
return true;
}
if (JSON.stringify(this.dataOld) != JSON.stringify(this.data)) {
const alert = await this.alertCtrl.create({
header: 'Unsaved Chnages',
message: 'Do you want to leave?',
buttons: [
{
text: 'No',
role: 'cancel',
handler: () => { }
},
{
text: 'Yes'),
role: 'goBack',
handler: () => { }
}
]
});
await alert.present();
let data = await alert.onDidDismiss();
if (data.role == 'goBack') {
return true;
} else {
return false;
}
} else {
return true;
}
}
要前进到PageB,我正在使用boolean byPassNav。在继续前进之前,我将此值设置为 TRUE,并且方法 canPageLeave 正在返回 TRUE。
向前导航在除以下情况外的一种情况下不起作用。
on PageA change some data and click on back button -> Confirmation pop up will open -> Select No -> Confirmation pop up will close and the same page remains open. Select button to move forward to PageB.
这会将导航移动到pageB,但也会将该页面设为根页面并删除所有路由历史记录。在此流程之后,我无法从 PageB 返回。
编辑:添加isDeactivatable的代码
export interface isDeactivatable {
canPageLeave: () => Observable<boolean> | Promise<boolean> | boolean;
}
【问题讨论】:
-
你能提供一个stackblitz最小表示你的问题吗?
-
我将此值设置为 TRUE。但您将其设置为 false,这是一个错字吗?
标签: angular ionic-framework routes ionic5 candeactivate