简体   繁体   English

在Ionic3中更改rootPage后未显示选项卡

[英]Tabs not showing after changing the rootPage in Ionic3

I generated an ionic 3 app using ionicCLI with tabs, added new page for login. 我使用带有选项卡的ionicCLI生成了ionic 3应用程序,并添加了用于登录的新页面。 I changed the the roofrpage to rootPage:any = LoginPage; 我将Roofrpage更改为rootPage:any = LoginPage; , when I load the home page unfortunately I can see the tabs. ,不幸的是,当我加载主页时,我可以看到选项卡。 How can I fix this error so that when I login I can be able to see the Homepage and any other pages that I will create to have the tabs? 如何解决此错误,以便在登录时可以看到主页以及将创建的带有标签的任何其他页面?

import { Component } from '@angular/core';
import { Platform } from 'ionic-angular';
import { StatusBar } from '@ionic-native/status-bar';
import { SplashScreen } from '@ionic-native/splash-screen';

import { TabsPage } from '../pages/tabs/tabs';
import { LoginPage } from '../pages/login/login';

@Component({
  templateUrl: 'app.html'
})
export class MyApp {
  //For it to load login first
  rootPage:any = LoginPage;
  //rootPage:any = TabsPage;

  constructor(platform: Platform, statusBar: StatusBar, splashScreen: SplashScreen) {
    platform.ready().then(() => {
      // Okay, so the platform is ready and our plugins are available.
      // Here you can do any higher level native things you might need.
      statusBar.styleDefault();
      splashScreen.hide();
    });
  }
}

Thanks 谢谢

In order to see the tabs, you have to set the rootPage to TabsPage . 为了查看选项卡,必须将rootPage设置为TabsPage The TabsPage is kind of like a wrapper around the pages that you have inside the tabs.ts . TabsPage有点像tabs.ts内的页面的包装。 So if you want to display the HomePage WITHOUT tabs, you would do this.rootPage = HomePage , if you want to have tabs you have to do this.rootPage = TabsPage . 因此,如果要显示没有标签的HomePage,则应执行this.rootPage = HomePage ,如果要具有标签,则必须执行this.rootPage = TabsPage

Usually what you want to do is assign the LoginPage when the user first opens the app and isn't logged in. (That way there will be no tabs, which is good because the user is not logged in and shouldn't be able to navigate). 通常,您要做的是在用户首次打开应用程序且未登录时为其分配LoginPage。(这样就不会有选项卡,这很好,因为用户尚未登录并且不应该能够导航)。 After a successful login, you set this.rootPage = TabsPage . 成功登录后,设置this.rootPage = TabsPage That will replace the current view with your tabs view. 这样会将当前视图替换为选项卡视图。 If you want to change the tabs / pages that are available here, you have to edit your tabs page here https://github.com/ionic-team/ionic2-starter-tabs/blob/master/src/pages/tabs/tabs.ts 如果要更改此处可用的选项卡/页面,则必须在此处编辑选项卡页面https://github.com/ionic-team/ionic2-starter-tabs/blob/master/src/pages/tabs/ tabs.ts

EDIT: 编辑:

To make it clearer. 使其更清楚。 You can also set the rootPage using this.nav.setRoot(TabsPage); 您还可以使用this.nav.setRoot(TabsPage);设置this.nav.setRoot(TabsPage); . So in your LoginPage you can have code that lets the user log in and in case of a successful callback, you set the root and it will load the HomePage (first tab on your TabsPage) 因此,在LoginPage中,您可以使用允许用户登录的代码,并在成功回调的情况下,设置根目录,它将加载HomePage(TabsPage上的第一个标签)

After doing research online I was able to figure out how to use rootPage:any = TabsPage; 在线研究之后,我能够弄清楚如何使用rootPage:any = TabsPage; ,

import { Component } from '@angular/core';
import { Platform } from 'ionic-angular';
import { StatusBar } from '@ionic-native/status-bar';
import { SplashScreen } from '@ionic-native/splash-screen';

import { TabsPage } from '../pages/tabs/tabs';
import { LoginPage } from '../pages/login/login';

@Component({
  templateUrl: 'app.html'
})
export class MyApp {
  rootPage:any = TabsPage;

  constructor(platform: Platform, statusBar: StatusBar, splashScreen: SplashScreen) {
    platform.ready().then(() => {
      // Okay, so the platform is ready and our plugins are available.
      // Here you can do any higher level native things you might need.
      statusBar.styleDefault();
      splashScreen.hide();
    });
  }
}

Then on the specific page hide the tabs. 然后在特定页面上隐藏选项卡。 This was achieved by this in that specific page .ts file I would like to hide the tab. 这是通过在我要隐藏该标签的特定页面.ts文件中实现的。

  tabBarElement: any;

  constructor(public navCtrl: NavController, public navParams: NavParams) {
    this.tabBarElement = document.querySelector('.tabbar.show-tabbar');
  }

  ionViewWillEnter(){
    this.tabBarElement.style.display = 'none';
  }

  ionViewWillLeave(){
    this.tabBarElement.style.display = 'flex';
  }

First of all, we are getting the tab bar element and storing it a variable called tabBarElement . 首先,我们获取选项卡栏元素并将其存储在名为tabBarElement的变量中。 Then we are hooking into the NavControllers lifecycle hooks. 然后,我们进入了NavControllers生命周期挂钩。 You can read more on lifecycle events here . 您可以在此处阅读有关生命周期事件的更多信息

The ionViewWillEnter() method will be called when the view is about to be shown so we are hiding the tab bar by doing this.tabBarElement.style.display = 'none'; 当视图将要显示时,将调用ionViewWillEnter()方法,因此我们通过执行this.tabBarElement.style.display = 'none';来隐藏标签栏this.tabBarElement.style.display = 'none'; .

Similarly, we want to unhide the tab bar element when the view is about to leave, we so that using ionViewWillLeave() and we set the display property to flex by doing this.tabBarElement.style.display = 'flex'; 同样,我们要在视图即将离开时取消隐藏选项卡栏元素,因此,我们使用ionViewWillLeave()并通过执行this.tabBarElement.style.display = 'flex';将display属性设置为flex this.tabBarElement.style.display = 'flex'; , By doing just this we are hiding the tab bar effectively. 通过这样做,我们有效地隐藏了标签栏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM