首页 文章

Ionic 4替代platform.registerBackButtonAction

提问于
浏览
1

我环顾了Ionic 4的新平台,似乎 registerBackButtonAction 功能已被删除 .

还有其他替代方法来处理Android硬件后退按钮吗?

1 回答

  • 2

    Update: 这是在dfac9dc中修复的


    Related: how to integrate hardware back button into ionic4 navigation


    这是在GitHubIonic ForumsTwitter上跟踪的
    在有正式修复之前,您可以使用此解决方法:

    this.platform.backButton.subscribe(() => {
      // code that is executed when the user pressed the back button
    })
    

    请注意,如果您想再次取消订阅,则需要保存subscribe(...)的结果 .


    Old answer: (截止日期为2018年4月)

    对于corresponding Cordova callregisterBackButtonActionjust a wrapper .

    所以你可以把你的旧电话转到 registerBackButtonAction

    this.platform.registerBackButtonAction(() => { 
      // code that is executed when the user pressed the back button
    });
    

    并替换为:

    this.platform.ready().then(() => {
      document.addEventListener("backbutton", () => { 
        // code that is executed when the user pressed the back button
      });
    });
    

相关问题