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

问题描述 投票:1回答:1

我想知道在离子3中默认单击离子导航栏后退按钮时调用了哪个函数。我想在硬件后退按钮上单击调用相同的功能。

ionic-framework button ionic2 ionic3 back
1个回答
3
投票

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

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();
          }
      });
    });
  }
}

希望这会帮助你。

© www.soinside.com 2019 - 2024. All rights reserved.