当前位置:首页 > VUE

vue实现底部tab

2026-01-19 03:00:05VUE

使用 Vue Router 实现底部 Tab

在 Vue 项目中,可以通过 vue-router 结合自定义组件实现底部 Tab 导航。创建一个 TabBar 组件,并在路由配置中定义每个 Tab 对应的页面。

<template>
  <div class="tab-bar">
    <router-link 
      v-for="(item, index) in tabs" 
      :key="index" 
      :to="item.path" 
      class="tab-item"
    >
      <span>{{ item.name }}</span>
    </router-link>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tabs: [
        { name: '首页', path: '/home' },
        { name: '分类', path: '/category' },
        { name: '购物车', path: '/cart' },
        { name: '我的', path: '/profile' }
      ]
    }
  }
}
</script>

<style scoped>
.tab-bar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  display: flex;
  height: 50px;
  background: #fff;
  border-top: 1px solid #eee;
}

.tab-item {
  flex: 1;
  display: flex;
  justify-content: center;
  align-items: center;
}

.router-link-active {
  color: #ff6700;
}
</style>

配置 Vue Router

在路由配置文件中,为每个 Tab 页面定义对应的路由。

import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '@/views/Home.vue'
import Category from '@/views/Category.vue'
import Cart from '@/views/Cart.vue'
import Profile from '@/views/Profile.vue'

Vue.use(VueRouter)

const routes = [
  { path: '/home', component: Home },
  { path: '/category', component: Category },
  { path: '/cart', component: Cart },
  { path: '/profile', component: Profile },
  { path: '/', redirect: '/home' }
]

const router = new VueRouter({
  routes
})

export default router

动态高亮当前 Tab

通过 $route.path 判断当前路由,动态添加高亮样式。

<template>
  <div class="tab-bar">
    <div 
      v-for="(item, index) in tabs" 
      :key="index" 
      @click="changeTab(item.path)"
      :class="['tab-item', { 'active': $route.path === item.path }]"
    >
      <span>{{ item.name }}</span>
    </div>
  </div>
</template>

<script>
export default {
  methods: {
    changeTab(path) {
      this.$router.push(path)
    }
  }
}
</script>

<style scoped>
.active {
  color: #ff6700;
}
</style>

使用第三方 UI 库

可以借助第三方库如 vant 快速实现底部 Tab。

npm install vant
import { Tabbar, TabbarItem } from 'vant'
Vue.use(Tabbar).use(TabbarItem)
<template>
  <van-tabbar v-model="active">
    <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>
export default {
  data() {
    return {
      active: 0
    }
  }
}
</script>

注意事项

  • 使用 position: fixed 固定底部 Tab 位置,确保内容区域不会被遮挡。
  • 路由配置中设置默认重定向,确保首次加载显示正确的 Tab 页面。
  • 移动端项目需考虑适配问题,可使用 remvw/vh 单位。

vue实现底部tab

标签: vuetab
分享给朋友:

相关文章

vue实现多级表头

vue实现多级表头

Vue 实现多级表头的方法 使用 el-table 组件 Element UI 的 el-table 组件支持多级表头的实现,通过嵌套 el-table-column 即可完成。 <el-ta…

vue实现文字播放栏

vue实现文字播放栏

Vue 实现文字播放栏(跑马灯效果) 方法一:使用 CSS 动画 + Vue 数据绑定 通过 CSS 的 @keyframes 实现动画效果,结合 Vue 的动态数据绑定控制内容。 <tem…

vue实现安全免登录

vue实现安全免登录

Vue 实现安全免登录的方案 免登录功能通常通过持久化认证状态(如 Token)实现,但需兼顾安全性。以下是基于 Vue 的常见实现方案: 基于 Token 的免登录 登录成功后存储 Toke…

vue中如何实现循环

vue中如何实现循环

在 Vue 中实现循环 Vue 提供了 v-for 指令用于实现循环渲染列表数据。v-for 可以遍历数组、对象或数字范围,生成动态的 DOM 元素。 遍历数组 使用 v-for 遍历数组时,语法…

vue实现右下角弹框

vue实现右下角弹框

实现右下角弹框的基本思路 在Vue中实现右下角弹框,通常需要结合CSS定位和Vue的组件化特性。弹框可以通过绝对定位固定在右下角,并通过Vue控制其显示与隐藏。 创建弹框组件 新建一个Vue组件(…

vue实现a4打印模板

vue实现a4打印模板

使用Vue实现A4打印模板 在Vue中实现A4打印模板需要结合CSS的打印样式和Vue的模板渲染能力。以下是具体实现方法: 设置A4纸张尺寸 在CSS中定义A4纸张的标准尺寸(210mm × 29…