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>
相关推荐
十八朵郁金香10 分钟前
通俗易懂的DOM1级标准介绍
开发语言·前端·javascript
计算机-秋大田36 分钟前
基于Spring Boot的兴顺物流管理系统设计与实现(LW+源码+讲解)
java·vue.js·spring boot·后端·spring·课程设计
m0_528723811 小时前
HTML中,title和h1标签的区别是什么?
前端·html
Dark_programmer1 小时前
html - - - - - modal弹窗出现时,页面怎么能限制滚动
前端·html
GDAL1 小时前
HTML Canvas clip 深入全面讲解
前端·javascript·canvas
禾苗种树1 小时前
在 Vue 3 中使用 ECharts 制作多 Y 轴折线图时,若希望 **Y 轴颜色自动匹配折线颜色**且无需手动干预,可以通过以下步骤实现:
前端·vue.js·echarts
贵州数擎科技有限公司2 小时前
使用 Three.js 实现流光特效
前端·webgl
JustHappy2 小时前
「我们一起做组件库🌻」做个面包屑🥖,Vue的依赖注入实战💉(VersakitUI开发实录)
前端·javascript·github
祝鹏2 小时前
前端如何制定监控项
前端
祝鹏2 小时前
原生开发监控告警指标设置
前端