vue3+element-plus 实现动态菜单和动态路由的渲染

目录的数据结构,其中还包括二级目录

复制代码
import { useRoute, useRouter } from 'vue-router'
import MenuTree from './components/MenuTree.vue'

const route = useRoute()
const router = useRouter()
const subchildren = ref(目录数据)

<el-menu
        active-text-color="#88733C"
        background-color="#e3ddd0"
        :default-active="$route.path"
        text-color="#808080"
        router
      >
        <MenuTree :menuList="subchildren"></MenuTree>
</el-menu>

子组件MenuTree:

复制代码
<script setup>
const props = defineProps(['menuList'])
</script>
<template>
  <template v-for="item in props.menuList" :key="item.id">
    <!-- {{ item }} -->
    <!--      分为两种方式渲染:有子菜单和没有子菜单-->
    <el-sub-menu :index="item.name" v-if="item.children.length !== 0">
      <template #title>
        <span>{{ item.title }}</span>
      </template>
      <!--        有子菜单的继续遍历(递归)-->
      <MenuTree :menuList="item.children"></MenuTree>
    </el-sub-menu>
    <!--      没有子菜单-->
    <el-menu-item :index="item.name" v-else>
      <span>{{ item.title }}</span>
    </el-menu-item>
  </template>
</template>
<style lang="scss" scoped></style>
相关推荐
xiaominlaopodaren几秒前
three.js地图数学基础(三):实战墨卡托
javascript·gis·three.js
雪碧聊技术6 分钟前
力扣 回溯法 | LCR 020. 回文子串
javascript·算法·leetcode
敲敲敲敲暴你脑袋29 分钟前
升级啦!纯前端打包下载离线地图
javascript·gis·canvas
SendTomo33 分钟前
send.wang私传网:P2P直连传大文件首选工具
javascript·网络·网络协议·webrtc·p2p
雨田言炎40 分钟前
十、QThread多线程
linux·服务器·开发语言·前端·qt
IT_陈寒40 分钟前
Vite热更新失效?我的几个犯傻操作害我debug两小时
前端·人工智能·后端
晓得迷路了43 分钟前
栗子前端技术周刊第 141 期 - Next.js 16.3、npm 安全事件、2026 CSS 现状调查报告结果...
前端·javascript·npm
2601_965798471 小时前
Launch Your Wellness App Fast: The Truth About Meditation Source Code
前端·macos·ios·objective-c·cocoa
周小董1 小时前
[1367]npm 安装 canvas 报错 node-gyp ERR
前端·npm·node.js
智脑API1 小时前
Codex config.toml check_for_update_on_startup 怎么关闭?离线环境与版本提醒排查
运维·服务器·前端