【问题标题】:Close the app by hardware back button on Ionic 4通过 Ionic 4 上的硬件后退按钮关闭应用程序
【发布时间】:2019-12-16 04:10:58
【问题描述】:

我正在尝试通过后退按钮关闭我的应用 我在github中找到了这段代码

 //Called when view is loaded as ionViewDidLoad() removed from Ionic v4
  ngOnInit(){
    this.initializeBackButtonCustomHandler();
  }


  //Called when view is left
  ionViewWillLeave() {
    // Unregister the custom back button action for this page
    this.unsubscribeBackEvent && this.unsubscribeBackEvent();
  }

  initializeBackButtonCustomHandler(): void {
    this.unsubscribeBackEvent = this.platform.backButton.subscribeWithPriority(999999,  () => {
        alert("back pressed home" + this.constructor.name);
    });
    /* here priority 101 will be greater then 100 
    if we have registerBackButtonAction in app.component.ts */
  }

它有效,但在所有页面上
我只想在主页上限制它
我用了this.constructor.name == HomePage,但也没有用

【问题讨论】:

    标签: cordova ionic-framework ionic4


    【解决方案1】:

    上面的代码有效,但我认为你写错了地方。在主页的 OnInit 或 ionViewDidEnter 方法上订阅 BackButton 并在 OnDestroy 或 ionPageWillLeave 上取消订阅。

    否则:您不必需要如此高的优先级。默认值为零。每个更高的都会工作。如果您在多个页面和组件上使用此方法,您应该考虑优先逻辑。

    【讨论】:

    • 我在“ionPageWillLeave”上取消了订阅,它工作正常
    猜你喜欢
    • 2021-10-15
    • 2020-12-08
    • 2019-12-28
    • 2023-03-19
    • 2020-01-12
    • 1970-01-01
    • 2017-12-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多