案例如下:我有一个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
;
我做错了什么?有人可以帮助我吗?
答案 0 :(得分:3)
尝试使用*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
不显示标签。
答案 1 :(得分:3)
在组件中创建一个tabs数组,并使用它来动态加载模板。然后,您可以轻松地操作组件中的tabs数组:
<强> tabs.ts 强>
content = response.query.pages.<page number>.extract
<强> tabs.html 强>
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);
}
});
}
}
答案 2 :(得分:0)
不是将变量值设置为tab1Root,而是将其设置为函数,以便每次到达变量时执行它。
this.tab1Root = () => this.variable?Page1:Page2;
答案 3 :(得分:0)
我在离子论坛上回答: 它不会起作用。这就是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
}
}
答案 4 :(得分:0)
现在:
IonicModule.forRoot(MyApp, {
tabsHideOnSubPages: true
})