我在Ionic 2中创建了一个sidemenu应用程序,其中包含一个主选项卡和3个子选项卡页面 . 它看起来像这样:
这是Main标签页的代码:
<ion-header>
<ion-navbar #content color="black">
<button ion-button menuToggle>
<ion-icon name="menu"></ion-icon>
</button>
<ion-title >Main Tab</ion-title>
</ion-navbar>
</ion-header>
<ion-tabs [selectedIndex]="mySelectedIndex" #myTabs>
<ion-tab [root]="tabARoot" tabTitle="Tab A" tabIcon="information-circle"></ion-tab>
<ion-tab [root]="tabBRoot" tabTitle="Tab B" tabIcon="information-circle"></ion-tab>
<ion-tab [root]="tabCRoot" tabTitle="Tab C" tabIcon="information-circle"></ion-tab>
</ion-tabs>
它包含3个子标签页,上面有一些乱码 .
这就是我的侧边菜单的样子 .
因此,当用户从侧面菜单中单击选项卡B链接时,他应该导航到主选项卡页面,其中选项卡B为选中状态 . 但是现在当我点击时,默认选择标签A.
是否有可能改变这种行为?
我的app.component.ts文件看起来像这样
import { Component, ViewChild } from '@angular/core';
import { Nav, Platform, App, Tabs } from 'ionic-angular';
import { StatusBar } from '@ionic-native/status-bar';
import { SplashScreen } from '@ionic-native/splash-screen';
import { MainPage } from '../pages/main/main';
@Component({
templateUrl: 'app.html'
})
export class MyApp {
@ViewChild(Nav) nav: Nav;
rootPage: any = MainPage;
pages: Array<{title: string, component: any}>;
constructor(public platform: Platform, public statusBar: StatusBar, public splashScreen: SplashScreen, private app: App) {
this.initializeApp();
// used for an example of ngFor and navigation
this.pages = [
{ title: 'Tab A', component: MainPage },
{ title: 'Tab B', component: MainPage },
{ title: 'Tab C', component: MainPage },
];
}
initializeApp() {
this.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.
this.statusBar.styleDefault();
this.splashScreen.hide();
});
}
openPage(page) {
// Reset the content nav to have just this page
// we wouldn't want the back button to show in this scenario
this.nav.setRoot(page.component);
}
}
从某个地方我得到的解决方案显然不起作用 . 在那个解决方案中,有人提到这样做,如下所示,但它不起作用'
this.nav.setRoot(page.component, {tabIndex: 2});
2 回答
ion-tabs组件中有一个名为selectedIndex的属性,用于设置默认选项卡 . 由于您在单击主选项卡时传递tabIndex,因此可以执行以下操作
否则,如果要以编程方式从控制器中选择任何选项卡,可以执行此操作,首先获取选项卡的引用,然后可以使用select()函数通过传递索引来设置所需的选项卡
@john Doe
设置root page ='MenuPage'页面
尝试以下代码:` src/pages/menu/menu.html :
src/pages/menu/menu.ts:
src/pages/tabs/tabs.html
src/pages/tabs/tabs.ts
谢谢