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>
相关推荐
ego.iblacat2 分钟前
在 LNMP 平台中部署 Web 应用
android·前端·adb
浩宇软件开发9 分钟前
springBoot+Vue中华诗词学习后台管理系统
vue.js·spring boot·axios·element-plus·router
weixin1997010801610 分钟前
南网商城商品详情页前端性能优化实战
java·前端·性能优化
陈天伟教授13 分钟前
WEB应用安全与防护 - 实操案例 2:CSRF(跨站请求伪造)—— 伪造用户操作
前端·安全·xss
@PHARAOH23 分钟前
HOW - 依赖包版本 lock 维护策略(pnpm)
前端
SuperEugene26 分钟前
前端-后端-产品-项目-运维:互联网项目协作全流程解析
运维·前端·javascript
weixin1997010801627 分钟前
网易考拉商品详情页前端性能优化实战
java·前端·python·性能优化
A黄俊辉A30 分钟前
openlayers+vue初学注意点
前端·javascript·vue.js
南篱34 分钟前
从回调地狱到优雅异步:JavaScript 异步编程的完整演进之路
前端·javascript·面试
陆枫Larry37 分钟前
折叠屏“窗口化”导致的背景图错位:一次小程序样式问题的排查与修复
前端