如何覆盖 Ionic 3 中的硬件后退按钮操作?

How to override hardware back button action in Ionic 3?

我想知道在ionic 3中默认点击ion-navbar后退按钮时调用了哪个函数。 我想在硬件后退按钮点击时调用相同的功能。

您可以使用registerBackButtonAction of Platform Service。 您可以在 app.component.ts 中覆盖硬件后退按钮操作,如下所示。 记得在 Platform.ready().

之后调用 registerBackButtonAction
import { Platform, App } from 'ionic-angular';

@Component({
  templateUrl: 'app.html'

})
export class MyApp {

  constructor(public platform: Platform, private app: App) {

    this.platform.ready().then(() => {

      this.platform.registerBackButtonAction(() => {

          let nav = this.app.getActiveNav()

          if (nav.canGoBack()) {
            // If there are pages in navigation stack go one page back
            // You can change this according to your requirement
            nav.pop();

          } else {

            // If there are no pages in navigation stack you can show a message to app user
            console.log("You cannot go back");
            // Or else you can exit from the app
            this.platform.exitApp();
          }
      });
    });
  }
}

希望对您有所帮助。