Tabbar 标签栏

引入

  1. import Vue from 'vue';
  2. import { Tabbar, TabbarItem } from 'vant';
  3. Vue.use(Tabbar);
  4. Vue.use(TabbarItem);

代码演示

基础用法

v-model默认绑定选中标签的索引值,通过修改v-model即可切换选中的标签

  1. <van-tabbar v-model="active">
  2. <van-tabbar-item icon="home-o">标签</van-tabbar-item>
  3. <van-tabbar-item icon="search">标签</van-tabbar-item>
  4. <van-tabbar-item icon="friends-o">标签</van-tabbar-item>
  5. <van-tabbar-item icon="setting-o">标签</van-tabbar-item>
  6. </van-tabbar>
  1. export default {
  2. data() {
  3. return {
  4. active: 0
  5. }
  6. }
  7. }

通过名称匹配

在标签指定name属性的情况下,v-model的值为当前标签的name

  1. <van-tabbar v-model="active">
  2. <van-tabbar-item name="home" icon="home-o">标签</van-tabbar-item>
  3. <van-tabbar-item name="search" icon="search">标签</van-tabbar-item>
  4. <van-tabbar-item name="friends" icon="friends-o">标签</van-tabbar-item>
  5. <van-tabbar-item name="setting" icon="setting-o">标签</van-tabbar-item>
  6. </van-tabbar>
  1. export default {
  2. data() {
  3. return {
  4. active: 'home'
  5. }
  6. }
  7. }

提示信息

设置dot属性后,会在图标右上角展示一个小红点。设置info属性后,会在图标右上角展示相应的徽标

  1. <van-tabbar v-model="active">
  2. <van-tabbar-item icon="home-o">标签</van-tabbar-item>
  3. <van-tabbar-item icon="search" dot>标签</van-tabbar-item>
  4. <van-tabbar-item icon="friends-o" info="5">标签</van-tabbar-item>
  5. <van-tabbar-item icon="setting-o" info="20">标签</van-tabbar-item>
  6. </van-tabbar>

自定义图标

通过 icon 插槽自定义图标,可以通过 slot-scope 判断标签是否选中

  1. <van-tabbar v-model="active">
  2. <van-tabbar-item info="3">
  3. <span>自定义</span>
  4. <img
  5. slot="icon"
  6. slot-scope="props"
  7. :src="props.active ? icon.active : icon.inactive"
  8. >
  9. </van-tabbar-item>
  10. <van-tabbar-item icon="search">标签</van-tabbar-item>
  11. <van-tabbar-item icon="setting-o">标签</van-tabbar-item>
  12. </van-tabbar>
  1. export default {
  2. data() {
  3. return {
  4. active: 0,
  5. icon: {
  6. active: 'https://img.yzcdn.cn/vant/user-active.png',
  7. inactive: 'https://img.yzcdn.cn/vant/user-inactive.png'
  8. }
  9. }
  10. }
  11. }

自定义颜色

  1. <van-tabbar
  2. v-model="active"
  3. active-color="#07c160"
  4. inactive-color="#000"
  5. >
  6. <van-tabbar-item icon="home-o">标签</van-tabbar-item>
  7. <van-tabbar-item icon="search">标签</van-tabbar-item>
  8. <van-tabbar-item icon="freinds-o">标签</van-tabbar-item>
  9. <van-tabbar-item icon="setting-o">标签</van-tabbar-item>
  10. </van-tabbar>

监听切换事件

  1. <van-tabbar v-model="active" @change="onChange">
  2. <van-tabbar-item icon="home-o">标签1</van-tabbar-item>
  3. <van-tabbar-item icon="search">标签2</van-tabbar-item>
  4. <van-tabbar-item icon="freinds-o">标签3</van-tabbar-item>
  5. <van-tabbar-item icon="setting-o">标签4</van-tabbar-item>
  6. </van-tabbar>
  1. import { Notify } from 'vant';
  2. export default {
  3. methods: {
  4. onChange(index) {
  5. Notify({ type: 'primary', message: index });
  6. }
  7. }
  8. }

路由模式

标签栏支持路由模式,用于搭配vue-router使用。路由模式下会匹配页面路径和标签的to属性,并自动选中对应的标签

  1. <router-view />
  2. <van-tabbar route>
  3. <van-tabbar-item
  4. replace
  5. to="/home"
  6. icon="home-o"
  7. >
  8. 标签
  9. </van-tabbar-item>
  10. <van-tabbar-item
  11. replace
  12. to="/search"
  13. icon="search"
  14. >
  15. 标签
  16. </van-tabbar-item>
  17. </van-tabbar>

API

Tabbar Props

参数说明类型默认值
v-model当前选中标签的名称或索引值string | number0
fixed是否固定在底部booleantrue
border是否显示外边框booleantrue
z-index元素 z-indexnumber1
active-color选中标签的颜色string#1989fa
inactive-color未选中标签的颜色string#7d7e80
route是否开启路由模式booleanfalse
safe-area-inset-bottom是否开启底部安全区适配,详细说明booleanfalse

Tabbar Events

事件名说明回调参数
change切换标签时触发active: 当前选中标签的名称或索引值

TabbarItem Props

参数说明类型默认值
name标签名称,作为匹配的标识符string | number当前标签的索引值
icon图标名称或图片链接,可选值见 Icon 组件string-
dot是否显示图标右上角小红点booleanfalse
info图标右上角徽标的内容string | number-
url点击后跳转的链接地址string-
to点击后跳转的目标路由对象,同 vue-router 的 to 属性string | object-
replace是否在跳转时替换当前页面历史booleanfalse

TabbarItem Slots

名称说明SlotProps
icon自定义图标active: 是否为选中标签

Tabbar 标签栏 - 图1