繁体   English   中英

防止在 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. } } ] }); })

我不知道当用户按下 no 时如何处理,即禁用后退按钮功能或事件。

最后我解决了这个问题。因为从 backButton 发出的事件是一个承诺。如果我不需要回去,我只是拒绝那个承诺。

 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.subscribeWithPriority(9999, () => {
      this.dismiss();
    });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM