当前位置:首页 > VUE

vue实现底部tab

2026-01-19 03:00:05VUE

使用 Vue Router 实现底部 Tab

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

vue实现底部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 页面定义对应的路由。

vue实现底部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 单位。

标签: vuetab
分享给朋友:

相关文章

vue实现流程

vue实现流程

Vue 实现流程的基本步骤 安装 Vue.js 可以通过 CDN 引入或使用 npm/yarn 安装。 CDN 方式: <script src="https://cdn.jsdelivr.…

vue树形实现

vue树形实现

Vue 树形组件实现方法 使用递归组件实现树形结构 递归组件是 Vue 中实现树形结构的常见方法。通过组件调用自身的方式,可以轻松构建多层嵌套的树形结构。 <template> &l…

vue实现通知

vue实现通知

Vue 实现通知功能的方法 使用 Vue 实现通知功能可以通过多种方式实现,以下是一些常见的方法: 使用 Vue 的全局事件总线 在 Vue 中可以通过事件总线实现通知功能。创建一个全局事件总线,然…

vue实现双向

vue实现双向

Vue 实现双向绑定的方法 Vue 的双向绑定主要通过 v-model 指令实现,它结合了数据绑定和事件监听,适用于表单元素(如 input、select、textarea 等)。以下是几种常见的实现…

vue实现书库

vue实现书库

Vue 实现书库功能 使用 Vue 构建书库功能需要结合前端框架和后端数据管理。以下是实现步骤和关键点: 项目初始化 使用 Vue CLI 或 Vite 初始化项目,安装 Vue Router 和状…

vue实现肢体识别

vue实现肢体识别

Vue 中实现肢体识别的方法 在 Vue 项目中实现肢体识别通常需要结合第三方库或 API,例如 TensorFlow.js、MediaPipe 或 OpenCV。以下是几种常见的实现方式: 使用…