Wang's blog Wang's blog
首页
  • 前端文章

    • HTML教程
    • CSS
    • JavaScript
  • 前端框架

    • Vue
    • React
    • VuePress
    • Electron
  • 后端技术

    • Npm
    • Node
    • TypeScript
  • 编程规范

    • 规范
  • 我的笔记
  • Git
  • GitHub
  • VSCode
  • Mac工具
  • 数据库
  • Google
  • 服务器
  • Python爬虫
  • 前端教程
更多
收藏
关于
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

Wang Mings

跟随大神,成为大神!
首页
  • 前端文章

    • HTML教程
    • CSS
    • JavaScript
  • 前端框架

    • Vue
    • React
    • VuePress
    • Electron
  • 后端技术

    • Npm
    • Node
    • TypeScript
  • 编程规范

    • 规范
  • 我的笔记
  • Git
  • GitHub
  • VSCode
  • Mac工具
  • 数据库
  • Google
  • 服务器
  • Python爬虫
  • 前端教程
更多
收藏
关于
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • CSS

  • Npm

  • Vue

    • Vue简介
    • 路由守卫
    • tsvue的写法
    • Vue常用技巧
    • vue的JSX写法
    • vue防抖组件
    • 动态热更新设置
    • keepActive缓存路由
    • 自动生成面包屑VUE组件
    • vue生命周期对比生命周期
    • eslint编译时警告或错误配置
    • Vue中封装axios的取消请求事情
    • vue+element递归生成无限菜单组件
    • Vue框架dist目录下各个文件的区别
    • 超详细Vue的种和Vue的种组件间通信方式
    • Vue项目中出现Loadingchunk{n}failed问题的解决方法
    • Vuex

    • 其他

    • 基础

    • 工具

    • 组件

    • 规模化

    • 过渡&动画

    • 可复用性&组合

  • HTML

  • Node

  • Yaml

  • React

  • 框架

  • 规范

  • Electron

  • JS演示

  • VuePress

  • JavaScript

  • TypeScript

  • 微信小程序

  • TypeScript-axios

  • 前端
  • Vue
wangmings
2022-07-19

vue+element递归生成无限菜单组件

# vue3+element递归生成无限菜单组件

直接上代码

<!-- 父组件 -->
<template>
    <el-menu
        text-color="#262626"
        active-text-color="#FF5A5F"
        :default-active="defaultActive"
        router
        >
        <MenuList v-model:menuList="menus.showedMenus" />
    </el-menu>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import MenuList from './components/menu-list.vue'

const router = useRouter()
// 菜单数组
const menus = [
    {
      id: 1,
      title: '数据预览',
      icon: 'el-icon-pie-chart',
      path: '/layout/chart',
    },
    {
      id: 2,
      title: '用户列表',
      icon: 'el-icon-user',
      path: '/layout/user',
    },
    {
      id: 3,
      title: '题库列表',
      icon: 'el-icon-edit-outline',
      path: '/layout/question',
      children: [
        {
            id: 4,
            title: '企业列表',
            icon: 'el-icon-office-building',
            path: '/layout/business',
        },
        {
            id: 5,
            title: '学科列表',
            icon: 'el-icon-notebook-2',
            path: '/layout/subject',
        },
      ]
    },
  ]
// 获取当前路由路径
const defaultActive = ref(router.currentRoute._rawValue.path)
</script>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<!-- 子组件 menu-list -->

<template>
  <template v-for="item in menuList" :key="item.label">
    <el-sub-menu v-if="item.children" :index="item.path || item.label">
      <template #title>
        <icon v-if="item.icon" :type="item.icon" />
        <span>{{ item.label }}</span>
      </template>
      <MenuList v-model:menuList="item.children" />
    </el-sub-menu>
    <el-menu-item v-else :index="item.path || item.label">
      <template #title>
        <icon v-if="item.icon" :type="item.icon" />
        <span>{{ item.label }}</span>
      </template>
    </el-menu-item>
  </template>
</template>

<script setup>
import { defineProps, defineAsyncComponent } from 'vue'
defineProps({
  menuList: {
    type: Array,
    require: true
  },
})

// 创建一个只有在需要时才会加载的异步组件。
// 引入自身组件,来做递归组件生成无限菜单
const MenuList = defineAsyncComponent({
  loader: () => import('@/views/layouts/user-center-layout/components/menu-list.vue'),
})
</script>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
编辑 (opens new window)
Vue中封装axios的取消请求事情
Vue框架dist目录下各个文件的区别

← Vue中封装axios的取消请求事情 Vue框架dist目录下各个文件的区别→

最近更新
01
theme-vdoing-blog博客静态编译问题
09-16
02
搜索引擎
07-19
03
友情链接
07-19
更多文章>
Theme by Vdoing | Copyright © 2019-2022 Evan Xu | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式