【问题标题】:Issue with pushing a new page from ion-tabs in ionic2从 ionic2 中的 ion-tabs 推送新页面的问题
【发布时间】:2018-04-13 10:32:27
【问题描述】:

我创建了一个示例聊天应用程序,当用户登录时,应用程序将导航到一个标签页

 <ion-tabs tabsPlacement="top" color="header" tabsHighlight=true>
  <ion-tab [root]="tab1" tabTitle="Chats" tabIcon="chatbubbles"></ion-tab>
  <ion-tab [root]="tab2" tabTitle="Groups" tabIcon="contacts"></ion-tab>
  <ion-tab [root]="tab3" tabTitle="Profile" tabIcon="contact"></ion-tab>
</ion-tabs>

  1. 当我通过console.log(this.navCtrl.getActive().name); 从标签中检查当前活动页面时,我得到undefined。你能告诉我为什么吗?

  2. 当我通过单击标题this.navCtrl.push('FriendsPage'); 中的应用程序图标导航到新页面时。在新页面中,当我打印当前活动页面名称时,它显示上一个页面名称“ChatPage”而不是“FriendsPage”

编辑:添加后退按钮操作

      this.platform.registerBackButtonAction(() => {
    let activePortal = this.ionicApp._loadingPortal.getActive() ||
      this.ionicApp._modalPortal.getActive() ||
      this.ionicApp._toastPortal.getActive() ||
      this.ionicApp._overlayPortal.getActive();
    if (activePortal) {
      activePortal.dismiss();
    } else {
      if (this.nav.canGoBack()) {
        this.nav.pop();
      } else {
        if (this.nav.getActive().name === "LoginPage"||this.nav.getActive().name === "SignupPage") {
          this.platform.exitApp();
        }else {
          this.generic.showAlertConfirm("Exit", "Do you want to exit the app?", this.onYesHandler, this.onNoHandler, "backPress");
        }
      }
    }
  })

在我的情况下,当用户从选项卡导航到 FriendsPage 并且按下硬件后退按钮时。在正常情况下,视图应该弹出。但我收到了警报消息。

this.generic.showAlertConfirm("Exit", "Do you want to exit the app?", this.onYesHandler, this.onNoHandler, "backPress");

请帮我解决这个问题。

感谢和问候

阿南德·拉杰

【问题讨论】:

  • 请帮我解决这个问题
  • 你在哪里使用console.log(...)?
  • 在两个页面的构造函数中

标签: angular typescript ionic2


【解决方案1】:

我遇到了和你一样的问题,但我仍然不知道为什么会这样。但我找到了一种获取正确页面名称的方法。

在app.component.ts 中添加以下代码:

import {  App } from 'ionic-angular';
constructor(private app: App){
}
ngAfterViewInit() { 
    this.app.getActiveNav().viewWillEnter.subscribe(event => {
      console.log("current page", event.id);  <== Here is what you need
    })
}

现在您可以在每个 viewWillEnter 事件中获取页面名称。无需在每个页面中添加代码。

【讨论】:

  • 让我检查一下,并会更新你。当我们点击返回按钮时问题就会出现
  • @AnandRaj:请不要在一篇文章中问两个不相关的问题!
  • 别难过,如果我按照上面的方法,后退按钮的功能将会中断。那是你。我使用方法 nav.getActive().name 实现了它
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-11
  • 2016-06-15
  • 2015-02-26
  • 1970-01-01
  • 2020-08-09
相关资源
最近更新 更多