当前位置:首页 > VUE

vue 实现菜单

2026-03-06 23:06:03VUE

Vue 实现菜单的方法

使用 v-for 动态渲染菜单项

通过 v-for 指令可以动态渲染菜单项,结合数组数据生成菜单结构。数据通常包含 namepath 字段,用于显示菜单名称和路由跳转。

<template>
  <div class="menu">
    <ul>
      <li v-for="item in menuItems" :key="item.path">
        <router-link :to="item.path">{{ item.name }}</router-link>
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  data() {
    return {
      menuItems: [
        { name: '首页', path: '/' },
        { name: '关于', path: '/about' },
        { name: '产品', path: '/products' }
      ]
    }
  }
}
</script>

嵌套菜单的实现

对于多级菜单,可以通过递归组件或嵌套 v-for 实现。数据中增加 children 字段表示子菜单。

<template>
  <div class="menu">
    <ul>
      <li v-for="item in menuItems" :key="item.path">
        <router-link :to="item.path">{{ item.name }}</router-link>
        <ul v-if="item.children">
          <li v-for="child in item.children" :key="child.path">
            <router-link :to="child.path">{{ child.name }}</router-link>
          </li>
        </ul>
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  data() {
    return {
      menuItems: [
        { 
          name: '首页', 
          path: '/',
          children: [
            { name: '子菜单1', path: '/sub1' },
            { name: '子菜单2', path: '/sub2' }
          ]
        },
        { name: '关于', path: '/about' }
      ]
    }
  }
}
</script>

使用 UI 框架快速实现

Element UI、Ant Design Vue 等框架提供了现成的菜单组件,可以快速实现复杂菜单功能。

以 Element UI 为例:

<template>
  <el-menu :default-active="activeIndex" mode="horizontal">
    <el-menu-item index="1">首页</el-menu-item>
    <el-submenu index="2">
      <template slot="title">产品</template>
      <el-menu-item index="2-1">产品1</el-menu-item>
      <el-menu-item index="2-2">产品2</el-menu-item>
    </el-submenu>
    <el-menu-item index="3">关于</el-menu-item>
  </el-menu>
</template>

<script>
export default {
  data() {
    return {
      activeIndex: '1'
    }
  }
}
</script>

菜单状态管理

通过 Vuex 或组件内状态管理当前激活的菜单项,实现高亮显示。

<template>
  <div class="menu">
    <ul>
      <li 
        v-for="item in menuItems" 
        :key="item.path"
        :class="{ active: currentPath === item.path }"
        @click="selectMenu(item.path)"
      >
        {{ item.name }}
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  data() {
    return {
      currentPath: '/',
      menuItems: [
        { name: '首页', path: '/' },
        { name: '关于', path: '/about' }
      ]
    }
  },
  methods: {
    selectMenu(path) {
      this.currentPath = path
      this.$router.push(path)
    }
  }
}
</script>

<style>
.active {
  color: red;
  font-weight: bold;
}
</style>

响应式菜单

结合媒体查询和 Vue 数据绑定,实现移动端和桌面端的不同菜单展示方式。

vue 实现菜单

<template>
  <div>
    <button @click="showMobileMenu = !showMobileMenu">菜单</button>
    <ul :class="{ 'mobile-menu': isMobile, 'show': showMobileMenu }">
      <li v-for="item in menuItems" :key="item.path">
        <router-link :to="item.path">{{ item.name }}</router-link>
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  data() {
    return {
      showMobileMenu: false,
      isMobile: window.innerWidth < 768,
      menuItems: [
        { name: '首页', path: '/' },
        { name: '关于', path: '/about' }
      ]
    }
  },
  mounted() {
    window.addEventListener('resize', this.handleResize)
  },
  methods: {
    handleResize() {
      this.isMobile = window.innerWidth < 768
    }
  }
}
</script>

<style>
.mobile-menu {
  display: none;
}
.mobile-menu.show {
  display: block;
}
@media (min-width: 768px) {
  .mobile-menu {
    display: block;
  }
}
</style>

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

相关文章

vue实现绑卡的原理

vue实现绑卡的原理

Vue 实现绑卡的原理 Vue 实现绑卡功能通常涉及前端表单处理、数据绑定、验证以及和后端 API 的交互。以下是实现绑卡功能的核心原理和步骤: 数据绑定与表单处理 Vue 通过 v-model 指…

vue实现grid

vue实现grid

Vue 实现 Grid 布局的方法 使用 CSS Grid 布局 Vue 可以结合 CSS Grid 布局实现灵活的网格系统。CSS Grid 是现代浏览器原生支持的布局方案,无需额外依赖库。 &l…

vue怎么实现

vue怎么实现

Vue 实现方法 Vue 是一个流行的前端框架,用于构建用户界面。以下是几种常见的实现方法: 数据绑定 使用 v-model 指令实现双向数据绑定,适用于表单输入元素。 <input v-m…

vue实现socket

vue实现socket

Vue 中实现 WebSocket 通信 在 Vue 项目中实现 WebSocket 通信可以通过原生 WebSocket API 或第三方库(如 socket.io-client)完成。以下是两种常…

vue实现素材

vue实现素材

Vue 实现素材的方法 使用 Vue 组件管理素材 在 Vue 项目中,可以通过组件化的方式管理素材。创建一个专门的组件来加载和显示素材,例如图片、视频或音频文件。组件可以接收素材的路径或 URL 作…

vue实现签章

vue实现签章

Vue 实现签章功能 签章功能通常包括手写签名、电子印章等。以下是基于 Vue 的实现方法: 使用 canvas 实现手写签名 安装依赖(如需要): npm install signature_p…