当前位置:首页 > VUE

vue实现菜单调用

2026-01-07 01:56:36VUE

Vue 实现动态菜单的常见方法

基于路由配置生成菜单

Vue Router 的路由配置可以天然转化为菜单结构,适合权限与路由绑定的系统:

// router/index.js
const routes = [
  {
    path: '/dashboard',
    name: 'Dashboard',
    component: Dashboard,
    meta: { title: '控制台', icon: 'el-icon-monitor' }
  },
  {
    path: '/user',
    name: 'User',
    component: User,
    meta: { title: '用户管理', icon: 'el-icon-user' },
    children: [
      { path: 'list', component: UserList, meta: { title: '用户列表' } }
    ]
  }
]
<!-- MenuComponent.vue -->
<template>
  <el-menu :router="true">
    <template v-for="route in $router.options.routes">
      <el-submenu 
        v-if="route.children" 
        :index="route.path"
        :key="route.path">
        <template #title>
          <i :class="route.meta.icon"></i>
          <span>{{ route.meta.title }}</span>
        </template>
        <el-menu-item 
          v-for="child in route.children" 
          :key="child.path"
          :index="route.path + '/' + child.path">
          {{ child.meta.title }}
        </el-menu-item>
      </el-submenu>
      <el-menu-item v-else :index="route.path" :key="route.path">
        <i :class="route.meta.icon"></i>
        <span>{{ route.meta.title }}</span>
      </el-menu-item>
    </template>
  </el-menu>
</template>

后端返回菜单数据方案

适合需要动态权限控制的系统,通过 API 获取菜单数据:

// 示例数据结构
[
  {
    id: 1,
    name: "System",
    title: "系统管理",
    icon: "setting",
    children: [
      { id: 11, name: "User", title: "用户管理", path: "/user" }
    ]
  }
]
<script>
export default {
  data() {
    return {
      menuItems: []
    }
  },
  async created() {
    const res = await axios.get('/api/menus')
    this.menuItems = res.data
  }
}
</script>

<template>
  <el-menu>
    <template v-for="item in menuItems">
      <el-submenu 
        v-if="item.children" 
        :index="item.id.toString()"
        :key="item.id">
        <template #title>
          <i :class="'el-icon-' + item.icon"></i>
          <span>{{ item.title }}</span>
        </template>
        <el-menu-item 
          v-for="child in item.children" 
          :key="child.id"
          :index="child.path">
          {{ child.title }}
        </el-menu-item>
      </el-submenu>
      <el-menu-item v-else :index="item.path" :key="item.id">
        <i :class="'el-icon-' + item.icon"></i>
        <span>{{ item.title }}</span>
      </el-menu-item>
    </template>
  </el-menu>
</template>

递归组件实现多级菜单

对于不确定层级的深度嵌套菜单,可使用递归组件:

<!-- RecursiveMenu.vue -->
<template>
  <el-submenu 
    v-if="item.children"
    :index="item.path">
    <template #title>
      <i :class="item.icon"></i>
      <span>{{ item.title }}</span>
    </template>
    <recursive-menu 
      v-for="child in item.children"
      :key="child.path"
      :item="child"/>
  </el-submenu>
  <el-menu-item v-else :index="item.path">
    <i :class="item.icon"></i>
    <span>{{ item.title }}</span>
  </el-menu-item>
</template>

<script>
export default {
  name: 'RecursiveMenu',
  props: ['item']
}
</script>

权限过滤实现

结合权限系统过滤显示的菜单项:

computed: {
  filteredMenus() {
    return this.menuItems.filter(menu => {
      return this.$hasPermission(menu.permission)
    })
  }
}

菜单状态保持

使用 Vuex 或 Pinia 存储菜单展开状态:

// store/modules/menu.js
const state = {
  openedMenus: ['1'] // 默认展开的菜单ID
}

const mutations = {
  TOGGLE_MENU(state, menuId) {
    const index = state.openedMenus.indexOf(menuId)
    if (index > -1) {
      state.openedMenus.splice(index, 1)
    } else {
      state.openedMenus.push(menuId)
    }
  }
}

响应式处理

监听路由变化高亮当前菜单项:

vue实现菜单调用

watch: {
  '$route.path': {
    immediate: true,
    handler(path) {
      this.$refs.menu.activeIndex = path
    }
  }
}

通过组合这些方法,可以构建出适应不同业务场景的 Vue 菜单系统。实际实现时应根据项目需求选择路由映射、后端动态配置或混合模式。

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

相关文章

vue实现飘窗原理

vue实现飘窗原理

vue实现飘窗原理 飘窗效果通常指在页面上悬浮显示的弹窗或通知,跟随页面滚动或固定在某个位置。Vue实现这一效果主要依赖组件化开发、CSS定位和动态数据绑定。 组件结构设计 创建一个独立的Vue组件…

vue实现动画

vue实现动画

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

vue实现选择

vue实现选择

Vue 实现选择功能的方法 在 Vue 中实现选择功能可以通过多种方式完成,以下介绍几种常见的实现方法。 使用 v-model 绑定单选 通过 v-model 可以轻松实现单选功能。以下是一个简单的…

vue实现api

vue实现api

Vue 实现 API 调用 在 Vue 中调用 API 通常涉及使用 axios 或 fetch 发送 HTTP 请求,并结合 Vue 的响应式特性管理数据。以下是常见的实现方式: 安装 axios…

vue实现追加

vue实现追加

追加数据到数组或列表 在Vue中追加数据到数组或列表,可以通过push方法或concat方法实现。以下是几种常见的实现方式: 方法一:使用push方法 this.items.push(newIte…

vue导航实现

vue导航实现

Vue 导航实现方法 在 Vue 中实现导航功能通常涉及路由配置、组件设计和状态管理。以下是几种常见的实现方式: 使用 Vue Router 实现基础导航 安装 Vue Router: npm i…