vue如何实现tabbar
Vue 实现 TabBar 的方法
使用 Vue Router 实现基础 TabBar
通过 Vue Router 的 router-link 和路由配置可以实现基础的 TabBar 功能。创建一个包含导航链接的组件,并通过 CSS 样式使其呈现为 TabBar 形式。
<template>
<div class="tab-bar">
<router-link to="/home" class="tab-item">首页</router-link>
<router-link to="/category" class="tab-item">分类</router-link>
<router-link to="/cart" class="tab-item">购物车</router-link>
<router-link to="/profile" class="tab-item">我的</router-link>
</div>
</template>
<script>
export default {
name: 'TabBar'
}
</script>
<style scoped>
.tab-bar {
display: flex;
position: fixed;
left: 0;
right: 0;
bottom: 0;
background: #f6f6f6;
box-shadow: 0 -1px 1px rgba(100,100,100,0.1);
}
.tab-item {
flex: 1;
text-align: center;
height: 49px;
line-height: 49px;
}
.router-link-active {
color: red;
}
</style>
在路由配置中设置对应的组件:
const routes = [
{ path: '/home', component: Home },
{ path: '/category', component: Category },
{ path: '/cart', component: Cart },
{ path: '/profile', component: Profile }
]
使用第三方 UI 库实现
许多 Vue UI 组件库提供了现成的 TabBar 组件,可以快速集成到项目中。例如使用 Vant 的 TabBar:

安装 Vant:
npm install vant
在项目中引入并使用:

<template>
<van-tabbar v-model="active" fixed>
<van-tabbar-item icon="home-o" to="/home">首页</van-tabbar-item>
<van-tabbar-item icon="search" to="/category">分类</van-tabbar-item>
<van-tabbar-item icon="cart-o" to="/cart">购物车</van-tabbar-item>
<van-tabbar-item icon="user-o" to="/profile">我的</van-tabbar-item>
</van-tabbar>
</template>
<script>
import { Tabbar, TabbarItem } from 'vant'
export default {
components: {
[Tabbar.name]: Tabbar,
[TabbarItem.name]: TabbarItem
},
data() {
return {
active: 0
}
}
}
</script>
自定义动态 TabBar
对于需要更灵活控制的场景,可以创建完全自定义的 TabBar 组件,通过 props 和事件实现动态配置。
<template>
<div class="custom-tabbar">
<div
v-for="(item, index) in tabs"
:key="index"
class="tab-item"
:class="{active: currentTab === index}"
@click="changeTab(index)"
>
<i :class="item.icon"></i>
<span>{{ item.title }}</span>
</div>
</div>
</template>
<script>
export default {
props: {
tabs: {
type: Array,
default: () => [
{ title: '首页', icon: 'icon-home' },
{ title: '分类', icon: 'icon-category' },
{ title: '购物车', icon: 'icon-cart' },
{ title: '我的', icon: 'icon-user' }
]
}
},
data() {
return {
currentTab: 0
}
},
methods: {
changeTab(index) {
this.currentTab = index
this.$emit('tab-change', index)
}
}
}
</script>
<style scoped>
.custom-tabbar {
display: flex;
position: fixed;
bottom: 0;
width: 100%;
background: #fff;
}
.tab-item {
flex: 1;
text-align: center;
padding: 8px 0;
}
.active {
color: #1989fa;
}
</style>
添加徽标和红点
在 TabBar 项目中添加徽标或红点提示可以增强用户体验。使用计算属性或监听数据变化来实现动态更新。
<template>
<div class="tab-bar">
<div
v-for="(item, index) in tabs"
:key="index"
class="tab-item"
@click="switchTab(index)"
>
<i :class="item.icon"></i>
<span class="tab-text">{{ item.text }}</span>
<span v-if="item.badge" class="badge">{{ item.badge }}</span>
<span v-if="item.dot" class="dot"></span>
</div>
</div>
</template>
<script>
export default {
data() {
return {
tabs: [
{ text: '首页', icon: 'icon-home', badge: '' },
{ text: '消息', icon: 'icon-message', dot: true },
{ text: '购物车', icon: 'icon-cart', badge: '5' },
{ text: '我的', icon: 'icon-user' }
]
}
},
methods: {
switchTab(index) {
// 切换逻辑
}
}
}
</script>
<style scoped>
.badge {
position: absolute;
top: 2px;
right: 10px;
padding: 0 4px;
background: red;
color: white;
border-radius: 10px;
font-size: 12px;
}
.dot {
position: absolute;
top: 5px;
right: 10px;
width: 6px;
height: 6px;
background: red;
border-radius: 50%;
}
</style>
这些方法提供了从基础到高级的 TabBar 实现方案,可以根据项目需求选择合适的实现方式。






