当前位置:首页 > 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 获取菜单数据:

vue实现菜单调用

// 示例数据结构
[
  {
    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>

权限过滤实现

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

vue实现菜单调用

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)
    }
  }
}

响应式处理

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

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

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

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

相关文章

vue项目实现

vue项目实现

Vue 项目实现指南 环境准备 确保已安装 Node.js(建议版本 14+)和 npm/yarn。通过以下命令检查版本: node -v npm -v 安装 Vue CLI(脚手架工具):…

vue怎么实现页面返回

vue怎么实现页面返回

Vue 实现页面返回的方法 在 Vue 中实现页面返回功能,可以通过以下几种方式完成,具体取决于项目使用的路由模式和技术栈。 使用 Vue Router 的编程式导航 通过 this.$route…

vue实现多层评论回复

vue实现多层评论回复

Vue 实现多层评论回复 数据结构设计 多层评论通常采用嵌套结构,每个评论对象包含子评论数组。示例数据结构如下: comments: [ { id: 1, content: '父…

前端vue登录功能实现

前端vue登录功能实现

Vue 登录功能实现步骤 创建登录表单组件 在 Vue 项目中创建一个登录组件,通常命名为 Login.vue。该组件包含用户名和密码输入框以及提交按钮。 <template> &…

vue实现打字机

vue实现打字机

Vue实现打字机效果 在Vue中实现打字机效果可以通过动态更新文本内容并添加延迟来实现。以下是几种常见的实现方法: 使用setInterval实现 <template> <di…

前端多线程实现vue

前端多线程实现vue

前端多线程与Vue的结合 在Vue中实现多线程通常通过Web Worker技术完成。Web Worker允许在后台线程中运行脚本,避免阻塞主线程,适合处理计算密集型任务。 使用Web Worker的…