【发布时间】:2018-02-21 21:34:16
【问题描述】:
我正在使用 Ionic 2。我认为在导航离开时应该提示用户确认他们想要离开(当时正在播放视频,可能是意外导航)。
当用户使用以下代码单击顶部导航中的后退按钮或后退硬件按钮 (Android) 时,我可以正常工作:
// About to leave
ionViewCanLeave() {
this.api.getDefaultMedia().pause();
return new Promise((resolve, reject) => {
if(!this.allowedToLeave) {
let confirm = this.alertCtrl.create({
title: 'Are you sure?',
message: 'If you leave a class you will need to start over. Are you sure you want to leave? If you need a break you can pause by tapping the video.',
buttons: [{
text: 'OK',
handler: () => {
this.allowedToLeave = true;
resolve();
},
}, {
text: 'Cancel',
handler: () => {
reject();
}
}],
});
confirm.present();
}
});
}
视图位于选项卡中。点击不同的标签不会调用这个函数,所以不会提示用户,标签只是切换。
如何在选项卡更改上也显示此提示?此视图不是根标签页。
--
我曾尝试使用ionViewWillLeave(),它是在标签更改时调用的,但它不允许阻止用户切换。下面的代码确实显示了提示,但是在标签改变之后:
// Called when user exits page via tab
ionViewWillLeave() {
this.api.getDefaultMedia().pause();
if(!this.allowedToLeave) {
let confirm = this.alertCtrl.create({
title: 'Are you sure?',
message: 'If you leave a class you will need to start over. Are you sure you want to leave? If you need a break you can pause by tapping the video.',
buttons: [{
text: 'OK',
handler: () => {
this.allowedToLeave = true;
this.leave();
},
}, {
text: 'Cancel',
handler: () => {
// Do nothing
}
}],
});
confirm.present();
return false;
}
}
// Leave the view
leave() {
this.navCtrl.pop();
}
【问题讨论】:
-
看来你需要回报承诺。本质上是
return confirm.present();而不是false。
标签: angular typescript ionic-framework tabs ionic2