vue3+elementui-plus实现无限递归菜单

效果图

实现方式是:通过给定的数据结构层数来动态生成多级菜单

复制代码
menu.vue

<template>
  <el-menu
      :default-active="activeIndex"
      class="el-menu-demo"
      mode="horizontal"
      @select="handleSelect"
      background-color="#f8f8f9"
      style="margin-top: 20px;margin-left: 1px;"
  >
    <Childrenmenu v-for="menuItem in menuItems" :key="menuItem.value" :item="menuItem" />
  </el-menu>
</template>

<script setup>
import Childrenmenu from "./childrenmenu";
const menuItems = [
	  {
	    value: '1',
	    label: '菜单1',
	    children: [
	      {
	        value: '1-1',
	        label: '子菜单1-1',
	        children: [
	          { value: '1-1-1', label: '子菜单1-1-1' },
	          { value: '1-1-2', label: '子菜单1-1-2' },
	        ],
	      },
	      { value: '1-2', label: '子菜单1-2' },
	    ],
	  },
	  {
	    value: '2',
	    label: '菜单2',
	    children: [
	      { value: '2-1', label: '子菜单2-1' },
	      {
	        value: '2-2',
	        label: '子菜单2-2',
	        children: [
	          { value: '2-2-1', label: '子菜单2-2-1' },
	          { value: '2-2-2', label: '子菜单2-2-2' },
	        ],
	      },
	    ],
	  },
	  {
	    value: '3',
	    label: '菜单3',
	    children: [
	      {
	        value: '3-1',
	        label: '子菜单3-1',
	        children: [
	          {
	            value: '3-1-1',
	            label: '子菜单3-1-1',
	            children: [
	              { value: '3-1-1-1', label: '子菜单3-1-1-1' },
	              { value: '3-1-1-2', label: '子菜单3-1-1-2' },
	            ],
	          },
	        ],
	      },
	    ],
	  },
	];
	
const handleSelect = async (key, keyPath) => {
	 console.log(key, keyPath)
}
</script>

childrenmenu.vue

<template>
  <template v-if="item.children">
    <el-sub-menu :index="item.value">
      <template #title>{{ item.label }}</template>
      <Childrenmenu v-for="childItem in item.children" :key="childItem.value" :item="childItem" />
    </el-sub-menu>
  </template>
  <template v-else>
    <el-menu-item :index="item.value">{{ item.label }}</el-menu-item>
  </template>
</template>

<script setup>
import { defineProps } from 'vue';

const props = defineProps(['item']);
</script>

<style scoped>

</style>
相关推荐
瑞瑞小同学13 分钟前
处理echarts x轴内容过多,重叠问题
前端·javascript·echarts
HexCIer19 分钟前
面向未来的原子化 CSS:UnoCSS 核心架构分析与 Tailwind CSS 现状
前端·css·vite
程序员黑豆39 分钟前
鸿蒙应用开发中的单位详解:px、vp、fp、lpx
前端·harmonyos
像我这样帅的人丶你还1 小时前
MCP + npm:给五年前的老系统接上AI
前端·javascript·agent
南一Nanyi1 小时前
依赖注入和控制反转
前端·设计模式·nestjs
小村儿1 小时前
连载14-实战篇--一个半月,我一个人和 Claude Code 搭出一套数字人工程
前端·后端·ai编程
愚公移码1 小时前
蓝凌EKP18产品:流程虚拟机(PVM)
java·开发语言·前端
promiseThen2 小时前
5 分钟上手 Markdown:标题到表格、代码块与简历实战
前端
web66liang2 小时前
webpack4+vue2项目使用 sass-embedded 导致的 DockerfIle 构建失败的问题
前端
Larcher2 小时前
LangChain RAG 排错实录:.env 为什么没有生效
vue.js·后端