使用ionic2 ionViewCanLeave Navigationguard 响应alert to stay or leave page的好处
Benefit of using ionic2 ionViewCanLeave Navigationguard to respond to alert to stay or leave page
目前看不到使用这种导航守卫的意义,因为我们不知道要导航到的目标页面(活动页面信息可从 navController getActive() 方法获得)。通常,我们会使用警报来询问问题 "Do you really want to leave this page?",以响应选择的菜单选项,然后就可以了,据我所知,我们不知道要 [=24= 哪个页面] 作为目标页面,因为 ionViewCanLeave 处理程序没有 toPage/enteringPage.
等参数
有没有人设法做到这一点,或者更确切地说,从 ionic2 目标页面信息中的导航对象中找到?
这里有一些设置上下文的代码:
ionViewCanLeave(): boolean { // no parameters on offer :-(
let result: boolean = false;
let alert: Alert = this._alertCtrl.create({
title: 'Are you sure you want to leave..?',
message: 'Your changes will be lost if you do?',
buttons: [
{
text: 'Cancel',
role: 'cancel',
handler: () => {
let dismiss: boolean = true;
return dismiss;
}
},
{
text: 'OK',
handler: () => {
let dismiss: boolean = false;
let alertPop = alert.dismiss();
alertPop
.then(() => {
let rootNav: NavController = this._appCtrl.getRootNav();
this._navCtrl.pop();
this._navCtrl.setRoot(DashboardPage); // Here we want to parameterize to target page - hardcoded for now
});
return dismiss;
}
}
]
});
if (this._applicationService.getOfferStatus() === OfferStatus.live) {
alert.present();
}
return result;
}
不是 100% 确定我理解问题...
但是据我了解,您需要根据某些条件阻止用户导航到某个页面。例如,在编辑表单上我有以下
ionViewCanLeave() {
// here we are checking if the dialog is being closed and there are unsaved changes
if (!this.saved && this.form.dirty) {
return new Promise((resolve: Function, reject: Function) => {
this.dialogs.showConfirmDialog('You have unsaved changes. Do you want to close and discard your changes?', null, null, 'confirm-warning-dialog')
.then((confirmed) => {
if (confirmed) {
resolve();
} else {
reject();
}
});
});
}
}
这会检查表单是否尚未保存并且是否进行了任何更改,它会显示一个确认对话框。如果在此对话框中单击取消,它会阻止导航,否则它会允许导航。 ionViewCanLeave 不会自行导航,而是在用户离开页面时充当处理程序。希望这有帮助
目前看不到使用这种导航守卫的意义,因为我们不知道要导航到的目标页面(活动页面信息可从 navController getActive() 方法获得)。通常,我们会使用警报来询问问题 "Do you really want to leave this page?",以响应选择的菜单选项,然后就可以了,据我所知,我们不知道要 [=24= 哪个页面] 作为目标页面,因为 ionViewCanLeave 处理程序没有 toPage/enteringPage.
等参数有没有人设法做到这一点,或者更确切地说,从 ionic2 目标页面信息中的导航对象中找到?
这里有一些设置上下文的代码:
ionViewCanLeave(): boolean { // no parameters on offer :-(
let result: boolean = false;
let alert: Alert = this._alertCtrl.create({
title: 'Are you sure you want to leave..?',
message: 'Your changes will be lost if you do?',
buttons: [
{
text: 'Cancel',
role: 'cancel',
handler: () => {
let dismiss: boolean = true;
return dismiss;
}
},
{
text: 'OK',
handler: () => {
let dismiss: boolean = false;
let alertPop = alert.dismiss();
alertPop
.then(() => {
let rootNav: NavController = this._appCtrl.getRootNav();
this._navCtrl.pop();
this._navCtrl.setRoot(DashboardPage); // Here we want to parameterize to target page - hardcoded for now
});
return dismiss;
}
}
]
});
if (this._applicationService.getOfferStatus() === OfferStatus.live) {
alert.present();
}
return result;
}
不是 100% 确定我理解问题... 但是据我了解,您需要根据某些条件阻止用户导航到某个页面。例如,在编辑表单上我有以下
ionViewCanLeave() {
// here we are checking if the dialog is being closed and there are unsaved changes
if (!this.saved && this.form.dirty) {
return new Promise((resolve: Function, reject: Function) => {
this.dialogs.showConfirmDialog('You have unsaved changes. Do you want to close and discard your changes?', null, null, 'confirm-warning-dialog')
.then((confirmed) => {
if (confirmed) {
resolve();
} else {
reject();
}
});
});
}
}
这会检查表单是否尚未保存并且是否进行了任何更改,它会显示一个确认对话框。如果在此对话框中单击取消,它会阻止导航,否则它会允许导航。 ionViewCanLeave 不会自行导航,而是在用户离开页面时充当处理程序。希望这有帮助