我怎样才能使离子装置后退按钮?(How can i enable device backbutton

2019-10-28 16:43发布

我对通过这样的后退按钮寄存器操作事件的一些条件禁用后退按钮:

    $ionicPlatform.registerBackButtonAction(function (event) {
    if (condition)
       {
       event.preventDefault();
       $ionicHistory.nextViewOptions({ disableBack: true });
       } 
    else
       {
       $ionicHistory.goBack();
       }
       }, 800);

所以,现在我如何能再次启用该设备后退按钮 ? 由于其仍处于禁用状态和以前的观点不会太。

Answer 1:

你需要尝试这个

var lastTimeBackPress = 0;
  var timePeriodToExit = 2000;

  platform.registerBackButtonAction(() => {
    // get current active page
    let view = this.nav.getActive();
    if (view.component.name == "HomePage") {
      //Double check to exit app                  
      if (new Date().getTime() - lastTimeBackPress < timePeriodToExit) {
        platform.exitApp(); //Exit from app
      } else {
        let toast = this.toastCtrl.create({
          message: 'Press back again to exit App',
          duration: 3000,
          position: 'bottom'
        });
        toast.present();
        lastTimeBackPress = new Date().getTime();
      }
    } else {
      // go to previous page
      this.nav.pop({});
    }
  });

希望它会为你工作



文章来源: How can i enable device backbutton in ionic?