我环顾了 Ionic 4 的新平台,似乎该 registerBackButtonAction功能已从中删除。
是否有其他方法可以处理 Android 硬件后退按钮?
我环顾了 Ionic 4 的新平台,似乎该 registerBackButtonAction功能已从中删除。
是否有其他方法可以处理 Android 硬件后退按钮?
更新:这已在v4.0.0-beta.8 (dfac9dc)中修复
这在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...)
})
请注意,subscribe(...)
如果您想再次取消订阅,则需要保存结果。
旧答案:(截至 2018 年 4 月已过期)
registerBackButtonAction只是相应 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",
有用!
离子 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);不是错误,它是解决方案的一部分:)