8

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

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

4

3 回答 3

28

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


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


这在GitHubIonic 论坛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
  });
});
于 2018-08-07T13:56:33.003 回答
1

我试穿了

"@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

于 2019-07-23T03:13:21.110 回答
0

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

import { Plugins } from '@capacitor/core'

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

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

于 2019-10-10T18:56:42.680 回答