当前位置:首页 > 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 项目中实现广告功能可以通过多种方式完成,具体取决于广告类型和需求。以下是几种常见的实现方法: 使用第三方广告平台 许多广告平台如 Google AdSense、百…

vue实现详情

vue实现详情

Vue 实现详情页的常见方法 使用动态路由 在 Vue Router 中配置动态路由参数,通过 $route.params 获取详情 ID。示例路由配置: { path: '/detail/:i…

vue el 实现

vue el 实现

Vue 中使用 Element UI (el) 的实现方法 Element UI 是一个基于 Vue 的组件库,提供丰富的 UI 组件,常用于快速构建企业级中后台产品。以下介绍 Vue 项目中集成和使…

vue nexttick实现

vue nexttick实现

Vue 中 nextTick 的实现原理 Vue 的 nextTick 是一个用于在下次 DOM 更新循环结束之后执行延迟回调的方法。它的核心实现依赖于 JavaScript 的事件循环机制。…

vue实现日历

vue实现日历

Vue 实现日历的基本方法 使用 Vue 实现日历可以通过自定义组件或借助第三方库完成。以下是两种常见实现方式: 自定义日历组件 创建基于 Vue 的日历组件需要处理日期计算和渲染逻辑。核心是生成当…

vue实现白板

vue实现白板

Vue实现白板功能 使用Vue实现白板功能可以通过HTML5的Canvas API结合Vue的响应式特性来完成。以下是实现白板功能的关键步骤: 安装依赖 在Vue项目中安装必要的依赖,如vue-dr…