Tabbar

Intro

Used to switch between different pages.

Install

Register component globally via app.use, refer to Component Registration for more registration ways.

import { createApp } from 'vue';
import { Tabbar, TabbarItem } from 'vant';

const app = createApp();
app.use(Tabbar);
app.use(TabbarItem);

Usage

Basic Usage

<van-tabbar v-model="active">
  <van-tabbar-item icon="home-o">Tab</van-tabbar-item>
  <van-tabbar-item icon="search">Tab</van-tabbar-item>
  <van-tabbar-item icon="friends-o">Tab</van-tabbar-item>
  <van-tabbar-item icon="setting-o">Tab</van-tabbar-item>
</van-tabbar>
import { ref } from 'vue';

export default {
  setup() {
    const active = ref(0);
    return { active };
  },
};

Match by name

<van-tabbar v-model="active">
  <van-tabbar-item name="home" icon="home-o">Tab</van-tabbar-item>
  <van-tabbar-item name="search" icon="search">Tab</van-tabbar-item>
  <van-tabbar-item name="friends" icon="friends-o">Tab</van-tabbar-item>
  <van-tabbar-item name="setting" icon="setting-o">Tab</van-tabbar-item>
</van-tabbar>
import { ref } from 'vue';

export default {
  setup() {
    const active = ref('home');
    return { active };
  },
};

Show Badge

<van-tabbar v-model="active">
  <van-tabbar-item icon="home-o">Tab</van-tabbar-item>
  <van-tabbar-item icon="search" dot>Tab</van-tabbar-item>
  <van-tabbar-item icon="friends-o" badge="5">Tab</van-tabbar-item>
  <van-tabbar-item icon="setting-o" badge="20">Tab</van-tabbar-item>
</van-tabbar>

Custom Icon

Use icon slot to custom icon.

<van-tabbar v-model="active">
  <van-tabbar-item badge="3">
    <span>Custom</span>
    <template #icon="props">
      <img :src="props.active ? icon.active : icon.inactive" />
    </template>
  </van-tabbar-item>
  <van-tabbar-item icon="search">Tab</van-tabbar-item>
  <van-tabbar-item icon="setting-o">Tab</van-tabbar-item>
</van-tabbar>
import { ref } from 'vue';

export default {
  setup() {
    const active = ref(0);
    const icon = {
      active: 'https://img.yzcdn.cn/vant/user-active.png',
      inactive: 'https://img.yzcdn.cn/vant/user-inactive.png',
    };
    return {
      icon,
      active,
    };
  },
};

Custom Color

<van-tabbar v-model="active" active-color="#ee0a24" inactive-color="#000">
  <van-tabbar-item icon="home-o">Tab</van-tabbar-item>
  <van-tabbar-item icon="search">Tab</van-tabbar-item>
  <van-tabbar-item icon="friends-o">Tab</van-tabbar-item>
  <van-tabbar-item icon="setting-o">Tab</van-tabbar-item>
</van-tabbar>

Change Event

<van-tabbar v-model="active" @change="onChange">
  <van-tabbar-item icon="home-o">Tab 1</van-tabbar-item>
  <van-tabbar-item icon="search">Tab 2</van-tabbar-item>
  <van-tabbar-item icon="friends-o">Tab 3</van-tabbar-item>
  <van-tabbar-item icon="setting-o">Tab 4</van-tabbar-item>
</van-tabbar>
import { ref } from 'vue';
import { Toast } from 'vant';

export default {
  setup() {
    const active = ref(0);
    const onChange = (index) => Toast(`Tab ${index}`);
    return {
      icon,
      onChange,
    };
  },
};

Route Mode

<router-view />

<van-tabbar route>
  <van-tabbar-item replace to="/home" icon="home-o">Tab</van-tabbar-item>
  <van-tabbar-item replace to="/search" icon="search">Tab</van-tabbar-item>
</van-tabbar>

API

Tabbar Props

Attribute Description Type Default
v-model Identifier of current tab number | string 0
fixed Whether to fixed bottom boolean true
border Whether to show border boolean true
z-index Z-index number | string 1
active-color Color of active tab item string #1989fa
inactive-color Color of inactive tab item string #7d7e80
route Whether to enable route mode boolean false
placeholder Whether to generate a placeholder element when fixed boolean false
safe-area-inset-bottom Whether to enable bottom safe area adaptation boolean false
before-change Callback function before changing tabreturn false to prevent changesupport return Promise (name: number | string) => boolean | Promise<boolean> -

Tabbar Events

Event Description Arguments
change Emitted when changing active tab active: number | string

TabbarItem Props

Attribute Description Type Default
name Identifier number | string Item index
icon Icon name string -
icon-prefix Icon className prefix string van-icon
dot Whether to show red dot boolean -
badge Content of the badge number | string ''
url Link string -
to Target route of the link, same as to of vue-router string | object -
replace If true, the navigation will not leave a history record boolean false

TabbarItem Slots

Name Description SlotProps
icon Custom icon active: boolean

CSS Variables

The component provides the following CSS variables, which can be used to customize styles. Please refer to ConfigProvider component.

Name Default Value Description
--van-tabbar-height 50px -
--van-tabbar-z-index 1 -
--van-tabbar-background-color var(--van-white) -
--van-tabbar-item-font-size var(--van-font-size-sm) -
--van-tabbar-item-text-color var(--van-gray-7) -
--van-tabbar-item-active-color var(--van-primary-color) -
--van-tabbar-item-active-background-color var(--van-white) -
--van-tabbar-item-line-height 1 -
--van-tabbar-item-icon-size 22px -
--van-tabbar-item-icon-margin-bottom var(--van-padding-base) -