当前位置:首页 > VUE

vue实现菜单

2026-02-09 09:34:42VUE

Vue 实现菜单的方法

使用 Vue Router 实现动态菜单

通过 Vue Router 可以轻松实现动态菜单,结合路由配置生成菜单项。以下是一个基本示例:

// router/index.js
const routes = [
  {
    path: '/home',
    name: 'Home',
    component: Home,
    meta: { title: '首页', icon: 'home' }
  },
  {
    path: '/about',
    name: 'About',
    component: About,
    meta: { title: '关于', icon: 'info' }
  }
]
<!-- Menu.vue -->
<template>
  <div class="menu">
    <router-link
      v-for="route in menuRoutes"
      :key="route.path"
      :to="route.path"
    >
      <i :class="route.meta.icon"></i>
      {{ route.meta.title }}
    </router-link>
  </div>
</template>

<script>
export default {
  computed: {
    menuRoutes() {
      return this.$router.options.routes.filter(route => route.meta)
    }
  }
}
</script>

使用递归组件实现多级菜单

对于多级菜单,可以使用递归组件实现:

<!-- MenuItem.vue -->
<template>
  <li>
    <div @click="toggle">
      {{ item.name }}
      <span v-if="hasChildren">{{ isOpen ? '-' : '+' }}</span>
    </div>
    <ul v-show="isOpen && hasChildren">
      <MenuItem
        v-for="child in item.children"
        :key="child.id"
        :item="child"
      />
    </ul>
  </li>
</template>

<script>
export default {
  name: 'MenuItem',
  props: {
    item: Object
  },
  data() {
    return {
      isOpen: false
    }
  },
  computed: {
    hasChildren() {
      return this.item.children && this.item.children.length
    }
  },
  methods: {
    toggle() {
      if (this.hasChildren) {
        this.isOpen = !this.isOpen
      }
    }
  }
}
</script>

使用 UI 框架实现菜单

主流 UI 框架如 Element UI、Ant Design Vue 都提供了现成的菜单组件:

<template>
  <el-menu
    :default-active="activeIndex"
    mode="horizontal"
    @select="handleSelect"
  >
    <el-menu-item index="1">首页</el-menu-item>
    <el-submenu index="2">
      <template #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>
</template>

<script>
export default {
  data() {
    return {
      activeIndex: '1'
    }
  },
  methods: {
    handleSelect(key, keyPath) {
      console.log(key, keyPath)
    }
  }
}
</script>

实现权限控制菜单

结合权限系统可以实现动态菜单渲染:

// 过滤有权限的菜单
function filterAsyncRoutes(routes, roles) {
  return routes.filter(route => {
    if (hasPermission(roles, route.meta?.roles)) {
      if (route.children) {
        route.children = filterAsyncRoutes(route.children, roles)
      }
      return true
    }
    return false
  })
}

响应式菜单实现

通过 CSS 媒体查询和 Vue 数据绑定可以实现响应式菜单:

vue实现菜单

<template>
  <div class="menu-container">
    <button @click="toggleMobileMenu">菜单</button>
    <nav :class="{ 'mobile-menu': isMobile, 'show': showMobileMenu }">
      <!-- 菜单内容 -->
    </nav>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isMobile: false,
      showMobileMenu: false
    }
  },
  mounted() {
    this.checkScreenWidth()
    window.addEventListener('resize', this.checkScreenWidth)
  },
  methods: {
    checkScreenWidth() {
      this.isMobile = window.innerWidth < 768
    },
    toggleMobileMenu() {
      this.showMobileMenu = !this.showMobileMenu
    }
  }
}
</script>

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

相关文章

vue调用接口实现退出

vue调用接口实现退出

实现Vue退出功能的步骤 在Vue中调用接口实现退出功能通常需要与后端API交互,以下是实现方法: 前端实现 创建退出方法,调用后端API接口: methods: { logout() {…

vue实现流程

vue实现流程

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

vue 实现直播

vue 实现直播

Vue 实现直播的基本方法 在Vue中实现直播功能,通常需要结合WebRTC、RTMP或HLS等技术。以下是几种常见的实现方式: 使用WebRTC实现实时直播 WebRTC适合低延迟的实时直播场景,…

vue实现过滤

vue实现过滤

Vue实现过滤的方法 在Vue中实现数据过滤可以通过多种方式完成,以下是几种常见的方法: 使用计算属性过滤 计算属性是Vue中处理数据过滤的理想选择,它们基于响应式依赖进行缓存,只有在相关依赖发生改…

vue评分实现

vue评分实现

Vue 评分组件实现方法 使用第三方库(如 Element UI) 安装 Element UI: npm install element-ui 引入并注册组件: import Vue from '…

vue实现动画

vue实现动画

Vue 实现动画的方法 Vue 提供了多种方式来实现动画效果,包括内置的过渡系统、第三方动画库以及直接操作 CSS 或 JavaScript 动画。 使用 Vue 内置过渡系统 Vue 的 <…