v-ons-tab

Represents a tab inside tab bar. Each <v-ons-tab> represents a page.

Tutorial

Tabs

The VOnsTabbar component is used to add tab navigation to an app. It is a very common navigation pattern in mobile apps.

The tabs prop includes all the necessary information to render VOnsTab components and their linked VOnsPage components. This prop must be an array of object containing at least one of the next keys: icon, label or page. It can include other optional keys such as activeIcon, badge, active, etc. A special props key can also be provided in order to specify props for the corresponding page.

<v-ons-tabbar
  :tabs="[
    { label: 't1', page: p1 },
    { label: 't2', page: p2, props: { aPageProp: 'hello' } }
  ]"
>
</v-ons-tabbar>

The mentioned tabs prop is the preferred way to provide tabs information to VOnsTabbar, although it is not the only one. It is also possible to provide slot="pages" and VOnsTab components directly as children instead:

<v-ons-tabbar>
  <template slot="pages">
    <home-page></home-page>
    <news-page></news-page>
    <settings-page></settings-page>
  </template>

  <v-ons-tab v-for="(tab, i) in tabs"
    :icon="tabs[i].icon"
    :label="tabs[i].label"
    :badge="tabs[i].badge"
    ></v-ons-tab>
</v-ons-tabbar>

This is a longer version that offers higher control. It can be combined with tabs prop by, for example, providing only VOnsTab-related information (everything except page key) in tabs prop and also slot="pages" at the same time (the VOnsTab will match page’s DOM index).

Synchronizing :index prop

The VOnsTabbar component implements an optional index prop which is used to specify the page that is currently visible. This component fires update:index events whenever the user taps on a VOnsTab. This is useful to synchronize the value of index prop and can be directly handled by using Vue’s sync modifier:

<v-ons-tabbar :index.sync="tabbarIndex"></v-ons-tabbar>

index prop is not completely necessary. If you don’t need to set an active tab in any other way than tapping, then it would be enough to provide active attribute (or key in tabs prop) to the desired VOnsTab in order to set the initial active tab.

The VOnsTab component

VOnsTab components have the following attributes/props:

  • icon: specifies the displayed icon.
  • label: specifies the displayed text label.
  • badge: shows a small badge on top of the tab.
  • activeIcon: allows to change the icon when the tab becomes active.
  • active: Whether the tab should be displayed as active or not. This is not necessary when using index prop.

Every tab has, by default, the same width. 50% with two tabs, 25% with four tabs and so on. To allow tabs grow depending on their content (i.e. shorter/ longer labels), use the autogrow modifier in v-ons-tabbar component. Optionally, max-width CSS property can be specified to set the width of the tab (for each v-ons-tab).

Swipes and Animations

By default, the tab bar will slide from one page to another on tab click. Use animation="none" attribute to have an instant change.

swipeable attribute can be used to enable this functionality. It can be toggled to allow or prevent swipes at different moments of the app.

These attributes can be combined to have a tab bar with instant changes that can also be swiped:

<v-ons-tabbar swipeable animation="none">...</v-ons-tabbar>

For iOS, tab-border attribute can be included to show a tab border that updates position during swipe (this is always default on Android).

Advanced usage

VOnsTab behavior can be overridden by running event.preventDefault on click event handler.

For example, this can provide fine control to support some of Vue’s cool features:

<v-ons-tabbar>
  <template slot="pages">
    <transition>
      <keep-alive>
        <component :is="currentPage"></component>
      </keep-alive>
    </transition>
  </template>

  <v-ons-tab
    @click.prevent="currentPage = 'home'"
    :active="currentPage === 'home'"
  ></v-ons-tab>
  <v-ons-tab
    @click.prevent="currentPage = 'settings'"
    :active="currentPage === 'settings'"
  ></v-ons-tab>
</v-ons-tabbar>

Notice that preventing the default behavior means that VOnsTabbar events (prechange, postchange, reactive…) are not fired. Also, it won’t be swipeable if only 1 page is provided at a time (when using :is="component", for example).

See also

Name Type Description
active Boolean This attribute should be set to the tab that is active by default. Optional.
active-icon String The name of the icon when the tab is active. Optional.
badge String Display a notification badge on top of the tab. Optional.
icon String The icon name for the tab. Can specify the same icon name as <v-ons-icon>. Check See also section for more information. Optional.
label String The label of the tab item. Optional.
page The page that is displayed when the tab is tapped. Optional.
Name Description
click Normal click event. Useful to modify the component default action (set an active page based on index).
click

Normal click event. Useful to modify the component default action (set an active page based on index).

Parameters
Name Type Description
event Object Event object.
event.preventDefault Function Avoids the default behavior.

Need Help?

If you have any questions, use our Community Forum or talk to us on Discord chat. The Onsen UI team and your peers in the community will work together to help solve your issues.

For bug reports and feature requests use our GitHub Issues page.