element递归渲染menu组件模板(vue3+js)

本文介绍了如何使用Vue.js构建一个可折叠的侧边栏菜单,展示了如何通过`v-for`遍历并递归渲染子菜单,以及如何利用`useRoute`和`useSidebarStore`进行路由和状态管理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、父siderbar

<template>
    <div class="sidebar">
        <el-menu class="sidebar-el-menu" :default-active="onRoutes" :collapse="sidebar.collapse" background-color="#324157"
            text-color="#bfcbd9" active-text-color="#20a0ff" unique-opened router>
            <template v-for="item in items">
                <template v-if="item.subs">
                    <SidebarItemVue :subItem="item" :key="item.index"/>
                </template>
                <template v-else>
                    <el-menu-item :index="item.index" :key="item.index">
                        <i :class="item.icon"></i>
                        <template #title>{{ item.title }}</template>
                    </el-menu-item>
                </template>
            </template>
        </el-menu>
    </div>
</template>

<script>
import { computed } from "vue";
import { useSidebarStore } from '../store/sidebar'
import { useRoute } from "vue-router";
import SidebarItemVue from "./SidebarItem.vue";
export default {
    setup() {
        const items = [
            {
                icon: "el-icon-lx-home",
                index: "/dashboard",
                title: "系统首页",
            },
            {
                icon: "el-icon-lx-cascades",
                index: "/table",
                title: "基础表格",
            },
            {
                icon: "el-icon-lx-copy",
                index: "/tabs",
                title: "tab选项卡",
            },
            {
                icon: "el-icon-lx-calendar",
                index: "3",
                title: "表单相关",
                subs: [
                    {
                        index: "/form",
                        title: "基本表单",
                    },
                    {
                        index: "/upload",
                        title: "文件上传",
                    },
                    {
                        index: "4",
                        title: "三级菜单",
                        subs: [
                            {
                                index: "/editor",
                                title: "富文本编辑器",
                            },
                            {
                                index: "/markdown",
                                title: "markdown编辑器",
                            },
                        ],
                    },
                ],
            },
            {
                icon: "el-icon-lx-emoji",
                index: "/icon",
                title: "自定义图标",
            },
            {
                icon: "el-icon-pie-chart",
                index: "/charts",
                title: "schart图表",
            },
            {
                icon: "el-icon-lx-global",
                index: "/i18n",
                title: "国际化功能",
            },
            {
                icon: "el-icon-lx-warn",
                index: "7",
                title: "错误处理",
                subs: [
                    {
                        index: "/permission",
                        title: "权限测试",
                    },
                    {
                        index: "/404",
                        title: "404页面",
                    },
                ],
            },
            {
                icon: "el-icon-lx-redpacket_fill",
                index: "/donate",
                title: "支持作者",
            },
        ];

        const route = useRoute();
        const onRoutes = computed(() => {
            return route.path;
        });

        const sidebar = useSidebarStore();

        return {
            items,
            onRoutes,
            sidebar,
        };
    },
    components:{
        SidebarItemVue
    }
};
</script>

<style scoped>
.sidebar {
    display: block;
    position: absolute;
    left: 0;
    top: 70px;
    bottom: 0;
    overflow-y: scroll;
}
.sidebar::-webkit-scrollbar {
    width: 0;
}
.sidebar-el-menu:not(.el-menu--collapse) {
    width: 250px;
}
.sidebar > ul {
    height: 100%;
}
</style>

2、子 siderbarItem

<template>
    <div class="sidebarItem">
        <el-submenu :index="subItem.index" :key="subItem.index">
            <template #title>
                <i :class="subItem.icon"></i>
                <span>{{ subItem.title }}</span>
            </template>
            <template v-for="items in subItem.subs">
                <template v-if="items.subs">
                    <!-- 递归渲染 -->
                    <sidebar-item  :key="items.index" :subItem="items"/>
                </template>
                <template v-else>
                    <el-menu-item :index="items.index" :key="items.index">{{ items.title }}</el-menu-item>
                </template>
            </template>
        </el-submenu>

    </div>
</template>

<script>

import { reactive ,toRefs } from 'vue'

export default {
    props:{
        subItem: {
            type: String,
            default: ''
        }
    },
    setup(props) {
        console.log(props,'2222')
        const data = reactive({
            subItem: props.subItem
        })

        return {
            ...toRefs(data)
        };
    },
};
</script>

<style scoped>

</style>

<think>好的,我现在需要帮助用户找到在Vue3中使用Element Plus实现递归菜单的示例或教程。首先,我需要回顾用户提供的引用内容,看看是否有相关的资料。 用户引用了四个来源,其中引用1、2、3和4都提到了VueElement Plus的递归菜单实现。特别是引用1、2、3详细介绍了递归组件的封装步骤和使用场景,而引用4讨论了vue-router与element-plus结合实现动态路由菜单的方法。这些信息应该足够指导用户完成递归菜单的实现。 接下来,我需要整理这些引用的关键点。比如,引用3提到了创建项目、引入库、封装递归组件等步骤,这可以作为教程的结构基础。引用2给出了使用示例,展示了如何传递菜单数据和设置属性,这可以作为代码示例的参考。引用1和4则强调了递归在动态菜单中的应用和与路由的结合,这部分需要包含在实现步骤中,特别是动态生成菜单和路由绑定的部分。 用户需要具体的示例或教程,所以应该分步骤说明,包括项目设置、组件封装、数据结构和路由配置等。每个步骤需要提供代码片段,并确保使用正确的LaTeX格式,比如变量名或代码块。同时,需要检查是否所有行内数学表达式都使用$...$,独立公式用$$...$$,但在这个问题中可能数学公式较少,主要关注代码和结构。 另外,用户可能关心如何动态生成菜单,如何处理多级嵌套,以及如何与vue-router集成。因此,在教程中需要涵盖递归组件的封装方法,如何通过props传递菜单数据,使用v-for循环渲染子菜单,以及点击菜单项跳转路由的实现。 最后,要生成相关问题,可能涉及性能优化、不同菜单模式(如水平或垂直)的实现,或者如何处理权限控制等。这些问题应该基于教程内容,帮助用户进一步探索相关主题。 现在需要确保回答结构清晰,分步骤说明,并正确引用提供的资料。同时,检查是否遵循了系统级指令,特别是LaTeX格式和引用标识的添加。</think>以下是在Vue3中结合Element Plus实现递归菜单的完整实现思路与示例代码: ### 一、实现递归菜单的核心步骤 1. **组件递归原理** 通过组件自身调用自身处理嵌套数据结构,需在组件内部定义`name`属性用于递归调用[^3]。 2. **菜单数据结构规范** 要求数据包含`children`字段表示子菜单项: ```typescript interface MenuItem { index: string title: string icon?: string children?: MenuItem[] } ``` 3. **组件封装流程** ```vue <!-- RecursiveMenu.vue --> <template> <el-sub-menu v-if="item.children" :index="item.index"> <template #title> <el-icon v-if="item.icon"><component :is="item.icon"/></el-icon> <span>{{ item.title }}</span> </template> <recursive-menu v-for="child in item.children" :key="child.index" :item="child" /> </el-sub-menu> <el-menu-item v-else :index="item.index"> <el-icon v-if="item.icon"><component :is="item.icon"/></el-icon> <span>{{ item.title }}</span> </el-menu-item> </template> <script setup lang="ts"> defineProps<{ item: MenuItem }>() </script> ``` ### 二、动态路由集成方案 1. **路由配置示例** ```typescript const routes = [ { path: &#39;/dashboard&#39;, component: Dashboard, meta: { menu: { index: &#39;1&#39;, title: &#39;控制台&#39;, icon: &#39;monitor&#39; } } }, { path: &#39;/user&#39;, meta: { menu: { index: &#39;2&#39;, title: &#39;用户管理&#39;, icon: &#39;user&#39;, children: [...] } } } ] ``` 2. **动态生成菜单** 通过路由信息自动构建菜单结构: ```vue <el-menu :router="true"> <recursive-menu v-for="route in filteredRoutes" :key="route.meta.menu.index" :item="route.meta.menu" /> </el-menu> ``` ### 三、典型应用场景 1. **权限管理系统** 根据用户权限动态过滤菜单项,实现差异化展示[^4] 2. **多级导航系统** 支持无限级嵌套的侧边栏/顶部导航菜单[^2] 3. **动态配置系统** 通过API获取菜单结构,实现可视化菜单配置 ### 四、完整实现示例 ```vue <!-- App.vue --> <template> <el-container> <el-aside width="200px"> <el-menu :default-active="activeIndex" mode="vertical" @select="handleSelect" > <recursive-menu v-for="item in menuData" :key="item.index" :item="item" /> </el-menu> </el-aside> <router-view /> </el-container> </template> <script setup lang="ts"> import { ref } from &#39;vue&#39; import { Document, Menu as IconMenu } from &#39;@element-plus/icons-vue&#39; const activeIndex = ref(&#39;1&#39;) const menuData = ref<MenuItem[]>([ { index: &#39;1&#39;, title: &#39;首页&#39;, icon: &#39;document&#39; }, { index: &#39;2&#39;, title: &#39;系统管理&#39;, icon: &#39;setting&#39;, children: [ { index: &#39;2-1&#39;, title: &#39;用户管理&#39;, icon: &#39;user&#39; } ] } ]) </script> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值