platform.registerBackButtonAction 的 Ionic 4 替代品

Ionic 4 alternative for platform.registerBackButtonAction

我查看了 Ionic 4 的新平台,似乎 registerBackButtonAction 功能已从中删除。

有没有其他方法可以处理 Android 硬件后退按钮?

更新: 这已在 v4.0.0-beta.8 (dfac9dc)

中修复


这是在 GitHub, in the Ionic Forums and 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...)
})

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


旧答案:(截至 2018 年 4 月已过时)

registerBackButtonActionjust a wrapper for the corresponding Cordova call.

所以你可以把你以前的电话转到 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
  });
});

我试穿了

"@ionic/angular": "^4.7.0-dev.201907191806.32b736e",
"@ionic/core": "^4.7.0-dev.201907191806.32b736e",

有效!

ionic git 提交 https://github.com/ionic-team/ionic/commit/978cc39009a9a0fb065540ce17e10c685b6c101a

如果是@ionic/vue,你可以把这个(或类似的东西)放在main.js

import { Plugins } from '@capacitor/core'

Plugins.App.addListener('backButton', function() {
  console.log(111);
  window.history.back();
});

不,不,console.log(111); 不是错误,它是解决方案的一部分:)