在 Ionic 4 App 中按下硬件后退按钮时防止返回
Prevent going back when hardware back button is pressed in Ionic 4 App
this.platform.backButton.subscribe(()=> {
const alert = await this.alertController.create({
header: 'Confirm!',
message: 'Do you want to go back!!!',
buttons: [
{
text: 'Yes',
handler: () => {
// Previous page loaded
}
}, {
text: 'No',
handler: () => {
//Page should not go back.
//This is where i want to write code,if the user clicks
No and the back button function should be disabled.
//Only when the user presses Yes,the page will go to
previous.
}
}
]
});
})
我不知道当用户按下否时如何处理,i.e.Disable 后退按钮功能或事件。
试试这个方法来防止后退按钮
this.platform.backButton.subscribeWithPriority(9999, () => {
this.dismiss();
});
最后我解决了 issue.As 从 backButton 发出的事件是 promise.If 我不需要返回,我只是拒绝那个承诺。
this.platform.backButton.subscribe(()=> {
const alert = await this.alertController.create({
header: 'Confirm!',
message: 'Do you want to go back!!!',
buttons: [
{
text: 'Yes',
handler: () => {
// Previous page loaded
}
}, {
text: 'No',
handler: () => {
reject()
}
}
]
});
})
this.platform.backButton.subscribe(()=> {
const alert = await this.alertController.create({
header: 'Confirm!',
message: 'Do you want to go back!!!',
buttons: [
{
text: 'Yes',
handler: () => {
// Previous page loaded
}
}, {
text: 'No',
handler: () => {
//Page should not go back.
//This is where i want to write code,if the user clicks
No and the back button function should be disabled.
//Only when the user presses Yes,the page will go to
previous.
}
}
]
});
})
我不知道当用户按下否时如何处理,i.e.Disable 后退按钮功能或事件。
试试这个方法来防止后退按钮
this.platform.backButton.subscribeWithPriority(9999, () => {
this.dismiss();
});
最后我解决了 issue.As 从 backButton 发出的事件是 promise.If 我不需要返回,我只是拒绝那个承诺。
this.platform.backButton.subscribe(()=> {
const alert = await this.alertController.create({
header: 'Confirm!',
message: 'Do you want to go back!!!',
buttons: [
{
text: 'Yes',
handler: () => {
// Previous page loaded
}
}, {
text: 'No',
handler: () => {
reject()
}
}
]
});
})