【问题标题】:How I can hide and show tabs conditionally - Ionic 2?我如何有条件地隐藏和显示标签 - Ionic 2?
【发布时间】:2017-04-03 21:14:33
【问题描述】:

案例如下:我有一个 ion-tabs 容器,其中包含多个 ion-tab 元素,以及登录到我的应用程序的不同用户。我需要做的是根据记录的用户类型显示或隐藏ion-tab 元素。

问题是我需要动态执行此操作,如果我使用像[show]="variable" 这样的指令,它就不起作用。

tabs.ts

import { Component } from '@angular/core';

import { Page1 } from '../page1/page1';
import { Page2 } from '../page2/page2';

@Component({
  templateUrl: 'tabs.html'
})
export class TabsPage {
  // this tells the tabs component which Pages
  // should be each tab's root Page
  tab1Root: any;
  tab2Root: any;
  tab3Root: any;
  tab4Root: any;
  tab5Root: any;
  variable: any;

  constructor(public userData: UserData) {
   // get variable from local storage
    this.userData.getUser().then((value) => {
      if(value.typeLabel == 'Expert') {
        this.variable = true;
        console.log('1->',value.typeLabel);
      }else if(value.typeLabel == 'Client'){
        this.variable = false;
        console.log('2->',value.typeLabel);
      }
    });

    this.tab1Root = this.variable?Page1:Page2; <-- i want to show specify tabs depending on variable value
    this.tab2Root = NotificationsPage;
    this.tab3Root = MessagesPage;
    this.tab4Root = InstructionsPage;
    this.tab5Root = ExpertsPage;
  }
}

tabs.html

<ion-tabs>
  <ion-tab [root]="tab1Root" tabTitle="Home" tabIcon="home"></ion-tab>
  <ion-tab [root]="tab2Root" tabTitle="tab_title" tabIcon="notifications"></ion-tab>
  <ion-tab [root]="tab3Root" tabTitle="tab_title" tabIcon="home"></ion-tab>
  <ion-tab [root]="tab4Root" tabTitle="tab_title" tabIcon="home"></ion-tab>
  <ion-tab [root]="tab5Root" tabTitle="tab_title" tabIcon="home"></ion-tab>
</ion-tabs>

但总是this.tab1Root返回Page1

我做错了什么?有人可以帮我吗?

【问题讨论】:

  • 你做到了吗?哪个答案对你有帮助?我也是同样的情况。
  • @VivekSinha,使变量 variable = true 以显示选项卡,使其为 false 不显示选项卡。在下方显示Mohan Gopi 响应。

标签: typescript tabs local-storage ionic2


【解决方案1】:

不要将变量值设置为 tab1Root,而是将其设置为一个函数,以便每次到达变量时执行它。

this.tab1Root = () => this.variable?Page1:Page2;

【讨论】:

    【解决方案2】:

    尝试使用*ngIf

    <ion-tabs *ngIf = "variable">
      <ion-tab [root]="tab1Root" tabTitle="Home" tabIcon="home"></ion-tab>
      <ion-tab [root]="tab2Root" tabTitle="tab_title" tabIcon="notifications"></ion-tab>
      <ion-tab [root]="tab3Root" tabTitle="tab_title" tabIcon="home"></ion-tab>
      <ion-tab [root]="tab4Root" tabTitle="tab_title" tabIcon="home"></ion-tab>
      <ion-tab [root]="tab5Root" tabTitle="tab_title" tabIcon="home"></ion-tab>
    </ion-tabs>
    

    在您的 .ts 文件中

    使变量variable = true 显示选项卡使其false 不显示选项卡。

    【讨论】:

    • 这可以工作,但是当动态更改为 true 时,将选项卡添加到选项卡的末尾,而不是他的正确位置...
    【解决方案3】:

    在您的组件中创建一个 tabs 数组并使用它来动态加载您的模板。然后,您可以轻松地操作组件中的 tabs 数组:

    tabs.ts

    import { Page1 } from '../page1/page1';
    import { Page2 } from '../page2/page2';
    
    @Component({
      templateUrl: 'tabs.html'
    })
    export class TabsPage {
    
      tabs: any[] = [
        { title: "Home", root: Page2, icon: "home" },
        { title: "tab_title", root: NotificationsPage, icon: "notifications" },
        { title: "tab_title", root: MessagesPage, icon: "home" },
        { title: "tab_title", root: InstructionsPage, icon: "home" },
        { title: "tab_title", root: ExpertsPage, icon: "home" }
      ];
    
      constructor(public userData: UserData) {
        // get variable from local storage
        this.userData.getUser().then((value) => {
          if(value.typeLabel == 'Expert') {
            this.tabs[0].root = Page1;
            console.log('1->',value.typeLabel);
          } else if(value.typeLabel == 'Client'){
            this.tabs[0].root = Page2;
            console.log('2->',value.typeLabel);
          }
        });
      }
    }
    

    tabs.html

    <ion-tabs>
      <ion-tab *ngFor="let tab of tabs" [root]="tab.root" [tabTitle]="tab.title" [tabIcon]="tab.icon"></ion-tab>
    </ion-tabs>
    

    【讨论】:

      【解决方案4】:

      正如我在 ionic 论坛中回答的那样: 它只是行不通。这就是 Ionic 的构建方式,它目前不允许动态设置标签根页面。我不得不折腾了几个小时,最后让它以下列方式工作: 1)获取选项卡组件的引用和要动态设置的选项卡的引用。不要为此动态选项卡设置 [root] 属性,因为 Ionic 将运行一个根本不允许您稍后更新此选项卡的根页面的方法。

      <ion-tabs #appTabs>
        <ion-tab #tabA tabTitle="Tab A" tabIcon="list-box"></ion-tab>
        <ion-tab [root]="tab2Root" tabTitle="Tab B" tabIcon="albums"></ion-tab>
        <ion-tab [root]="tab3Root" tabTitle="Tab C" tabIcon="pulse"></ion-tab>
        <ion-tab [root]="tab4Root" tabTitle="Tab D" tabIcon="more"></ion-tab>
      </ion-tabs>
      

      2) 在 TabsPage 文件中:

      import { Component, ViewChild } from '@angular/core';
      import { Store } from '@ngrx/store'; // I'm using ngrx
      import { Tabs, Tab } from 'ionic-angular';
      import { Observable } from 'rxjs/Observable';
      import 'rxjs/operator/filter';
      import 'rxjs/operator/map';
      
      import { TabA1Page } from '...';
      import { TabA2Page } from '...';
      import { TabBPage } from '...';
      import { TabCPage } from '...';
      import { TabDPage } from '...';
      import { AppState } from '../../app.reducer';
      import { DashboardCard } from '...';
      
      @Component({
        templateUrl: 'tabs.page.html',
        selector: 'page-tabs'
      })
      export class TabsPage {
        public tab1Root: any; // As you can see, it's not set yet.
        public tab2Root: any = TabBPage;
        public tab3Root: any = TabCPage;
        public tab4Root: any = TabDPage;
      
        // Get the references
        @ViewChild('appTabs') private tabsRef: Tabs;
        @ViewChild('tabA') private tabRef: Tab;
      
        constructor(private store: Store<AppState>) {}
      
        public ionViewDidEnter() {
          // These three sources can change the root page for the tab A:
          const defaultDashboard$ = this.store.select('task', 'fetchDashboardCards', 'value');
          const dashboardByProject$ = this.store.select('task', 'fetchDashboardCardsByProject', 'value');
          const dashboardByDueDate$ = this.store.select('task', 'fetchDashboardCardsByDueDate', 'value');
      
          Observable
            .merge(defaultDashboard$, dashboardByProject$, dashboardByDueDate$)
            .filter(v => v != null)
            .map((dashboard: DashboardCard[]) => dashboard[0].layout === 'project' ? TabA1 : TabA2)
            .distinctUntilChanged()
            .subscribe(this.setTabARootPage.bind(this));
        }
      
        private setTabARootPage(rootPage) {
          // This is the hack
      
          // set the root property that we didn't set in the view as [root]="..."
          this.tabRef.root = rootPage;
      
          /*
          * Modifying private attributes to make it work.
          * This will allow Ionic to run the "tabsInstance.load()" method to load a new root page
          * It also allows us to select the same tab again.
          */
          this.tabRef._loaded = false;
          this.tabsRef._tabs[0].isSelected = false;
      
          this.tabsRef.select(this.tabRef); // select the tab again
        }
      
      }
      

      【讨论】:

        【解决方案5】:

        现在:

        IonicModule.forRoot(MyApp, {
          tabsHideOnSubPages: true
        })
        

        【讨论】:

          猜你喜欢
          • 2017-08-18
          • 1970-01-01
          • 2022-08-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-09-01
          • 1970-01-01
          • 2016-04-30
          相关资源
          最近更新 更多