在Ionic 4 App中按下硬件后退按钮时防止返回

时间:2019-02-26 12:38:22

标签: javascript angular6 ionic4

    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时如何处理,即禁用后退按钮功能或事件。

2 个答案:

答案 0 :(得分:0)

尝试这种方式以防止后退按钮

this.platform.backButton.subscribeWithPriority(9999, () => {
      this.dismiss();
    });

答案 1 :(得分:0)

最后我解决了这个问题。由于从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()
              }
            }
         ]
      });
    })