当前位置:首页 > VUE

vue 底部菜单实现

2026-02-17 09:54:49VUE

实现 Vue 底部菜单的方法

使用 vant 组件库的 Tabbar

安装 vant 库后,可以直接使用 van-tabbar 组件实现底部菜单。

<template>
  <van-tabbar v-model="active" fixed>
    <van-tabbar-item icon="home-o">首页</van-tabbar-item>
    <van-tabbar-item icon="search">搜索</van-tabbar-item>
    <van-tabbar-item icon="friends-o">好友</van-tabbar-item>
    <van-tabbar-item icon="setting-o">设置</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>

自定义底部菜单

如果不想依赖 UI 库,可以手动实现一个底部菜单。

<template>
  <div class="bottom-menu">
    <div 
      v-for="(item, index) in menuItems" 
      :key="index" 
      class="menu-item"
      :class="{ 'active': activeIndex === index }"
      @click="activeIndex = index"
    >
      <span>{{ item.label }}</span>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      activeIndex: 0,
      menuItems: [
        { label: '首页' },
        { label: '搜索' },
        { label: '好友' },
        { label: '设置' },
      ],
    };
  },
};
</script>

<style>
.bottom-menu {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  display: flex;
  justify-content: space-around;
  background: #fff;
  padding: 10px 0;
  box-shadow: 0 -2px 5px rgba(0, 0, 0, 0.1);
}
.menu-item {
  padding: 5px 10px;
  cursor: pointer;
}
.menu-item.active {
  color: #1989fa;
  font-weight: bold;
}
</style>

结合路由实现页面切换

底部菜单通常需要配合 Vue Router 进行页面切换。

<template>
  <div class="bottom-menu">
    <router-link 
      v-for="(item, index) in menuItems" 
      :key="index" 
      :to="item.path"
      class="menu-item"
      active-class="active"
    >
      <span>{{ item.label }}</span>
    </router-link>
  </div>
</template>

<script>
export default {
  data() {
    return {
      menuItems: [
        { label: '首页', path: '/' },
        { label: '搜索', path: '/search' },
        { label: '好友', path: '/friends' },
        { label: '设置', path: '/settings' },
      ],
    };
  },
};
</script>

使用 element-uiel-menu

如果项目使用 element-ui,可以利用 el-menuhorizontal 模式实现底部菜单。

<template>
  <el-menu 
    mode="horizontal" 
    class="bottom-menu"
    :default-active="activeIndex"
    @select="handleSelect"
  >
    <el-menu-item index="1">首页</el-menu-item>
    <el-menu-item index="2">搜索</el-menu-item>
    <el-menu-item index="3">好友</el-menu-item>
    <el-menu-item index="4">设置</el-menu-item>
  </el-menu>
</template>

<script>
export default {
  data() {
    return {
      activeIndex: '1',
    };
  },
  methods: {
    handleSelect(index) {
      console.log('选中菜单:', index);
    },
  },
};
</script>

<style>
.bottom-menu {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  display: flex;
  justify-content: space-around;
}
</style>

注意事项

  • 固定定位:使用 position: fixed 确保菜单始终固定在底部。
  • 路由匹配:如果结合 Vue Router,确保 active-classactive 样式正确匹配当前路由。
  • 兼容性:在移动端需考虑 safe-area-inset-bottom 以避免被设备底部遮挡。

以上方法可根据项目需求选择适合的实现方式。

vue 底部菜单实现

标签: 菜单vue
分享给朋友:

相关文章

vue实现打印二维码

vue实现打印二维码

安装二维码生成库 使用 qrcode 或 vue-qrcode 库生成二维码。通过 npm 安装: npm install qrcode vue-qrcode 生成二维码 在 Vue 组件中引…

vue实现store

vue实现store

Vue 实现 Store 在 Vue 中,可以通过 Vuex 或 Pinia 实现全局状态管理(Store)。以下是两种主流方案的实现方法。 使用 Vuex 实现 Store Vuex 是 Vu…

vue实现主题

vue实现主题

Vue 实现主题的方法 使用 CSS 变量动态切换主题 定义全局 CSS 变量,通过修改这些变量实现主题切换。在 Vue 的根组件或全局样式中定义变量: :root { --primary-c…

vue实现拖放

vue实现拖放

Vue 实现拖放功能 Vue 中实现拖放功能通常可以通过 HTML5 的原生拖放 API 或第三方库(如 vuedraggable)来实现。以下是两种方法的详细说明。 使用 HTML5 原生拖放…

vue实现公告

vue实现公告

Vue 实现公告功能的方法 公告功能通常需要实现滚动展示、自动切换或固定显示的效果。以下是几种常见的实现方式: 使用 marquee 标签实现滚动公告 <template> <…

vue的实现

vue的实现

Vue 的实现原理 Vue 是一个渐进式 JavaScript 框架,其核心实现主要包括响应式系统、虚拟 DOM、模板编译和组件化机制。以下是 Vue 实现的主要技术细节。 响应式系统 Vue 的…