废话不多说直接上代码:
递归菜单项:
<template> <el-submenu v-if="menuData.children && menuData.children.length > 0" :index="menuData.menuId"> <template slot="title"> <i class="el-icon-location" /> <span>{{ menuData.menuDesc }}</span> </template> <MenuItem v-for="item in menuData.children" :key="item.menuId" :menu-data="item" /> </el-submenu> <el-menu-item v-else :index="menuData.menuId"> <i class="el-icon-menu" /> <span slot="title">{{ menuData.menuDesc }}</span> </el-menu-item> </template> <script> export default { name: 'MenuItem', // eslint-disable-next-line vue/require-prop-types props: ['menuData'], data() { return { // menuData: [{ menuId: '1', menuDesc: '导航1', parentMenu: null, children: [{ menuId: '1-1', menuDesc: '导航1-1', parentMenu: '1' }] }] } }, methods: { handleOpen(key, keyPath) { console.log(key, keyPath) }, handleClose(key, keyPath) { console.log(key, keyPath) } } } </script>
外层代码:
<el-menu default-active="1" mode="horizontal"> <menu-item v-for="item in menuList" :key="item.id" :menu-data="item" /> </el-menu> // 数据示例: menuList: [{ menuId: '1', menuDesc: '导航1', children: [{ menuId: '1-1', menuDesc: '导航1-1', children: null }] }, { menuId: '2', menuDesc: '导航2', children: [] }]
标签:el,elementui,menuId,menu,menuData,menuDesc,key,children,keyPath From: https://www.cnblogs.com/volts0302/p/17765653.html