Vue3使用递归组件封装El-Menu多级菜单

html 复制代码
<template>
    <aside class="menu">
        <el-scrollbar>
            <el-menu class="menu-main" router :default-active="route.path" unique-opened background-color="#18214C"
                text-color="#fff">
                <menuItem v-for="item in menuList" :item="item" :key="item.path" />
            </el-menu>
        </el-scrollbar>
    </aside>
</template> 
<script setup>
import menuItem from "./components/menuItem.vue"
import { useRoute } from "vue-router"
const route = useRoute()

const menuList = [
    {
        path: "/index",
        component: () => import("@/views/index.vue"),
        name: "index",
        meta: {
            title: "一级菜单"
        }
    },
    {
        path: "/level",
        name: "level",
        meta: {
            title: "多级菜单"
        },
        children: [
            {
                path: "/level/level-1",
                name: "level-1",
                meta: {
                    title: "多级菜单-1"
                },
                children: [
                    {
                        path: "/level/level-1/level-1-1",
                        component: () => import("@/views/level/level-1/level-1-1.vue"),
                        name: "level-1-1",
                        meta: {
                            title: "多级菜单-1"
                        }
                    }]
            },
            {
                path: "/level/level-2",
                component: () => import("@/views/level/level-2.vue"),
                name: "level-2",
                meta: {
                    title: "多级菜单-2"
                }
            }
        ]
    }
]


</script>
<style lang='scss' scoped>
.menu {
    height: 100vh;
    overflow: hidden;
    background-color: #18214C;

    &-main {
        border: none;
    }

    &-main:not(.el-menu--collapse) {
        width: 220px;
    }
}
</style>
html 复制代码
<template>
    <template v-if="item.meta && !item.meta.hidden">
        <el-sub-menu :index="item.path" :key="item.path" v-if="item?.children?.length > 0">
            <template #title>
                <el-icon>
                    <Aim />
                </el-icon>
                <span>{{ item.meta.title }}</span>
            </template>
            <menuItem v-for="child in item.children" :item="child" :key="child.path" />
        </el-sub-menu>
        <el-menu-item v-else :index="item.path">
            <el-icon>
                <Aim />
            </el-icon>
            <template #title>{{ item.meta.title }}</template>
        </el-menu-item>
    </template>
</template>
<script setup name="menuItem">
import { Aim } from '@element-plus/icons-vue'
const props = defineProps({
    item: {
        type: Object,
        default: () => { },
    },
})
</script>
<style lang='scss' scoped>
.el-menu-item.is-active {
    color: #fff;
    background: #2260FF;
}
</style>
相关推荐
yanglamei196214 分钟前
基于Python+Django+Vue的旅游景区推荐系统系统设计与实现源代码+数据库+使用说明
vue.js·python·django
[廾匸]26 分钟前
cesium视频投影
javascript·无人机·cesium·cesium.js·视频投影
流烟默1 小时前
vue和微信小程序处理markdown格式数据
前端·vue.js·微信小程序
梨落秋溪、1 小时前
输入框元素覆盖冲突
java·服务器·前端
菲力蒲LY1 小时前
vue 手写分页
前端·javascript·vue.js
一丢丢@zml1 小时前
new 一个构造函数的过程以及手写 new
javascript·手写new
天下皆白_唯我独黑2 小时前
npm 安装扩展遇到证书失效解决方案
前端·npm·node.js
~欸嘿2 小时前
Could not download npm for node v14.21.3(nvm无法下载节点v14.21.3的npm)
前端·npm·node.js
化作繁星2 小时前
React 高阶组件的优缺点
前端·javascript·react.js
zpjing~.~3 小时前
vue 父组件和子组件中v-model和props的使用和区别
前端·javascript·vue.js