Ionic 4替代platform.registerBackButtonAction

nim*_*mzz 5 ionic-framework ionic4

我环顾了Ionic 4的新平台,看起来该 registerBackButtonAction功能已被删除.

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

Fab*_* N. 19

更新:这已在dfac9dc中修复


相关:如何将硬件后退按钮集成到ionic4导航中


这是在GitHub,Ionic论坛和Twitter上跟踪的.
在有官方修复之前,您可以使用此解决方法:

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

// To prevent interference with ionic's own backbutton handling
// you can subscribe with a low priority instead
this.platform.backButton.subscribeWithPriority(0, () => {
  // code that is executed when the user pressed the back button
  // and ionic doesn't already know what to do (close modals etc...)
})
Run Code Online (Sandbox Code Playgroud)

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


旧答案:(截止到2018年4月)

registerBackButtonAction是只是一个包装的相应科尔多瓦通话.

所以你可以接受旧的电话registerBackButtonAction:

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

并替换为:

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