Select a specific tab in Ionic 2

强颜欢笑 提交于 2019-12-04 19:31:32

问题


I'm trying to use the Ionic 2 and I'm still struggling with most basic tasks, such as select a tab when the app is loading.

I've tried to inject the Tabs controller and call select on the onPageLoaded event, but to no avail.

Can someone help maybe?


回答1:


To default to a tab in ionic 2 change the selectedIndex property:

<ion-tabs [selectedIndex]="1">
      <ion-tab [root]="tab1Root" tabTitle="Home" tabIcon="rewind"></ion-tab> <!-- Index 0-->
      <ion-tab [root]="tab2Root" tabTitle="About" tabIcon="md-time"></ion-tab><!-- Index 1 (Selected)-->
      <ion-tab [root]="tab3Root" tabTitle="Contacts" tabIcon="fastforward"></ion-tab><!-- Index 2-->
</ion-tabs>

The About tab will be the selected tab when the page loads.




回答2:


//importing tabs for manipuling our ion-tabs
import {Tabs} from 'ionic-angular';
@Page({
    templateUrl: 'build/pages/page1/page1.html'
})
export class Page1 
{
    //provide Angular with metadata about things it should inject in the constructor
    static get parameters()
    {
        return [[Tabs]];
    }
    //after injecting ,passing an instance of [Tabs] in the page constructor 
    constructor(tab) {
        this.tab = tab;   
    }
    //"onPageWillEnter" function fires every time a page becomes the active      view.
    onPageWillEnter()
    {
        //make the second tab selected From the first tab (within the current Page 'page1')
        // 1 IS the index of the target tab
        this.tab.select(1);
    }
}



回答3:


try

var t: Tabs = this.nav.parent;
t.select(index);



回答4:


In Ionic 3 and angular 4.

import { Tabs } from 'ionic-angular/navigation/nav-interfaces';
@ViewChild('myTabs') tabRef: Tabs; - With in the class about constructor.
this.tabRef.select(0, {}); // In the method where you want set tab.


来源:https://stackoverflow.com/questions/34675922/select-a-specific-tab-in-ionic-2

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!