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>
相关推荐
孤水寒月3 小时前
基于HTML的悬窗可拖动记事本
前端·css·html
祝余呀3 小时前
html初学者第一天
前端·html
脑袋大大的4 小时前
JavaScript 性能优化实战:减少 DOM 操作引发的重排与重绘
开发语言·javascript·性能优化
速易达网络6 小时前
RuoYi、Vue CLI 和 uni-app 结合构建跨端全家桶方案
javascript·vue.js·低代码
耶啵奶膘6 小时前
uniapp+firstUI——上传视频组件fui-upload-video
前端·javascript·uni-app
JoJo_Way6 小时前
LeetCode三数之和-js题解
javascript·算法·leetcode
视频砖家6 小时前
移动端Html5播放器按钮变小的问题解决方法
前端·javascript·viewport功能
lyj1689977 小时前
vue-i18n+vscode+vue 多语言使用
前端·vue.js·vscode
小白变怪兽8 小时前
一、react18+项目初始化(vite)
前端·react.js
ai小鬼头8 小时前
AIStarter如何快速部署Stable Diffusion?**新手也能轻松上手的AI绘图
前端·后端·github