ionic 4 prevent/disable device hardware backbutton

我怕爱的太早我们不能终老 提交于 2020-01-24 13:53:27

问题


I'm using angular routing(@angular/router) for ionic 4 project to disable the device back-button in ionic 4 prevent-default is not working below is my code in

app.component.ts

    this.platform.backButton.subscribe(() => {
        if (this.router.url === '/Login') {
          this.util.presentAppExitAlert();
        } else {
          // event.preventDefault();
          console.log("invoing url ", this.router.url);
        }
      });
    });

i am not able to disable the device back-button any help here


回答1:


initializeApp() {
    this.platform.ready().then(() => {
      this.platform.backButton.subscribeWithPriority(9999, () => {
        document.addEventListener('backbutton', function (event) {
          event.preventDefault();
          event.stopPropagation();
          console.log('hello');
        }, false);
      });
      this.statusBar.styleDefault();
    });
  }



回答2:


I found out how to undo it (give back button previous functionality):

Your observer is pushed to the this.platform.backButton.observers array. So you just have to pop the last element of the list:

this.platform.backButton.observers.pop();

Hope it helps somebody.



来源:https://stackoverflow.com/questions/55274992/ionic-4-prevent-disable-device-hardware-backbutton

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!