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>

相关推荐

  1. Vue.js 3.0】组件实现思路

    2024-04-20 18:42:02       18 阅读
  2. Vue3+elementPlus组件

    2024-04-20 18:42:02       16 阅读
  3. 组件怎么实现无线滚动

    2024-04-20 18:42:02       27 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-04-20 18:42:02       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-04-20 18:42:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-04-20 18:42:02       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-04-20 18:42:02       20 阅读

热门阅读

  1. python图表用户界面(gui)的选择

    2024-04-20 18:42:02       12 阅读
  2. springboot 项目eureka注册中心切换为nacos+config

    2024-04-20 18:42:02       14 阅读
  3. 高频前端面试题汇总之手写代码篇

    2024-04-20 18:42:02       14 阅读
  4. Edge的使用心得与深度探索

    2024-04-20 18:42:02       17 阅读
  5. CPU执行过程

    2024-04-20 18:42:02       16 阅读
  6. Ansible离线安装

    2024-04-20 18:42:02       23 阅读
  7. Elasticsearch(1)

    2024-04-20 18:42:02       36 阅读
  8. eslint 规则

    2024-04-20 18:42:02       49 阅读