首页 > 解决方案 > 构建时platform.registerBackButtonAction()不起作用--prod

问题描述

我正在使用 ionic 3 进行项目。

ionic cordova run android我使用这个命令来运行应用程序。

在这个操作platform.registerBackButtonAction()中工作正常..

但是,如果我使用该ionic cordova run android --prod选项,platform.registerBackButtonAction ()则无法正常工作。

任何帮助表示赞赏。

下面是我处理硬件后退按钮的代码。

this.platform.registerBackButtonAction(() => {
    let view = this.nav.getActive();
    if(view.component.name == "NonetworkPage"){
      if (!this.showedAlert) {
        this.confirmExitApp();
      } else {
        this.showedAlert = false;
        this.confirmAlert.dismiss();
      }
    }else{
      if (view.component.name == "HomePage") {
        if (!this.showedAlert) {
          this.confirmExitApp();
        } else {
          this.showedAlert = false;
          this.confirmAlert.dismiss();
        }
      } else if (view.component.name != "HomePage" && view.component.name != "LoginPage") {
        if (this.nav.length() == 1) {
          this.nav.setRoot(HomePage);
        } else if (this.nav.length() > 1) {
          this.nav.pop();
        }
      } else if (view.component.name == "LoginPage") {
        this.confirmExitApp();
      }
    }
  });

以下是确认退出弹出窗口

 confirmExitApp() {
    this.showedAlert = true;
    this.confirmAlert = this.alertCtrl.create({
      title: "Exit App?",
      message: "Are you sure you want to exit App?",
      enableBackdropDismiss: true,
      cssClass: 'confirmCustomCss',
      buttons: [
        {
          text: 'No',
          handler: () => {
            this.showedAlert = false;
            return;
          }
        },
        {
          text: 'Yes',
          handler: () => {
            this.platform.exitApp();
          }
        }
      ]
    });
    this.confirmAlert.present();
  }

标签: ionic-frameworkionic2ionic3ionic-native

解决方案


因为 prod 标志会缩小我们的代码并混淆页面名称,但是您可以通过使用此代码来解决此问题,可能希望它对您有所帮助,并且它将在 build --prod 和 build 上运行

 platform.registerBackButtonAction(() => {
    let view = this.navCtrl.getActive();
    let page = view ? this.navCtrl.getActive().instance : null;
    if (page && (page instanceof Mypage)  

不要忘记将 Mypage 导入 app.component.ts


推荐阅读