当前位置:   article > 正文

vue3 element-plus动态菜单及动态图标_element plus 动态菜单

element plus 动态菜单

1. 安装element-plus及图标库

  1. # NPM
  2. $ npm install @element-plus/icons-vue
  3. # Yarn
  4. $ yarn add @element-plus/icons-vue
  5. # pnpm
  6. $ pnpm install @element-plus/icons-vue
  7. # NPM
  8. $ npm install element-plus --save
  9. # Yarn
  10. $ yarn add element-plus
  11. # pnpm
  12. $ pnpm install element-plus

2.全局引入

引入element-plus

  1. //引入element-plus
  2. import ElementPlus from 'element-plus'
  3. const app = createApp(App)
  4. app.use(ElementPlus)

注册图标组件

  1. // main.ts
  2. // 如果您正在使用CDN引入,请删除下面一行。
  3. import * as ElementPlusIconsVue from '@element-plus/icons-vue'
  4. const app = createApp(App)
  5. for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
  6. app.component(key, component)
  7. }

3. 动态菜单代码

动态引入图标代码

  1. <el-icon>
  2. <component v-if="item.meta" :is="item.meta.icon"></component>
  3. </el-icon>

完整代码

  1. <template>
  2. <el-menu
  3. active-text-color="#ffd04b"
  4. background-color="#545c64"
  5. text-color="#fff"
  6. default-active="2"
  7. router
  8. class="el-menu-vertical-demo"
  9. :collapse="props.isCollapse"
  10. :ellipsis="false"
  11. @open="handleOpen"
  12. @close="handleClose"
  13. >
  14. <!-- <el-sub-menu index="/home">
  15. <template #title>
  16. <el-icon><location /></el-icon>
  17. <span>常用功能</span>
  18. </template>
  19. <el-menu-item index="/throttle">节流</el-menu-item>
  20. <el-menu-item index="/debounce">防抖</el-menu-item>
  21. </el-sub-menu>
  22. <el-menu-item index="2">
  23. <el-icon><icon-menu /></el-icon>
  24. <template #title>Navigator Two</template>
  25. </el-menu-item>
  26. <el-menu-item index="3" disabled>
  27. <el-icon><document /></el-icon>
  28. <template #title>Navigator Three</template>
  29. </el-menu-item>
  30. <el-menu-item index="4">
  31. <el-icon><setting /></el-icon>
  32. <template #title>Navigator Four</template>
  33. </el-menu-item> -->
  34. <template v-for="item in menuRoutes" :key="item.id">
  35. <el-sub-menu v-if="item.children && item.children.length > 0" :index="item.path">
  36. <template #title>
  37. <el-icon>
  38. <component v-if="item.meta" :is="item.meta.icon"></component>
  39. </el-icon>
  40. <!-- <el-icon><location /></el-icon> -->
  41. <span v-if="item.meta">{{item.meta.title}}</span>
  42. </template>
  43. <!-- 二级菜单 -->
  44. <el-menu-item v-for="item2 in item.children" :key="item2.id" :index="item2.path" :style="item2.children?'display: none': null">
  45. {{item2.children? null : item2.meta.title}}
  46. </el-menu-item>
  47. <!-- 三级菜单 -->
  48. <el-sub-menu v-for="item2 in item.children.filter(x => x.children && x.children.length > 0)" :key="item2.id" class="child-sub-menu" :index="item2.path">
  49. <template #title>
  50. <!-- <component v-if="item.meta" :is="item.meta.icon" style="width: 20px; height: 20px"></component> -->
  51. <!-- <el-icon><location /></el-icon> -->
  52. <span v-if="item2.meta">{{item2.meta.title}}</span>
  53. </template>
  54. <el-menu-item v-for="item3 in item2.children" :key="item3.id" :index="item3.path">{{item3.meta.title}}</el-menu-item>
  55. </el-sub-menu>
  56. <!-- </template> -->
  57. </el-sub-menu>
  58. <el-menu-item v-else :index="item.path">{{item.meta.title}}</el-menu-item>
  59. </template>
  60. </el-menu>
  61. </template>
  62. <script lang="ts" setup>
  63. import { ref, onMounted } from 'vue'
  64. import { useRouter } from 'vue-router'
  65. import { menuRoutes } from '@/router/index'
  66. const router = useRouter()
  67. const props = defineProps({
  68. isCollapse: {
  69. type: Boolean,
  70. default: false,
  71. }
  72. })
  73. const emits = defineEmits(['menuChange'])
  74. const handleOpen = (key: string, keyPath: string[]) => {
  75. console.log(key, keyPath)
  76. }
  77. const handleClose = (key: string, keyPath: string[]) => {
  78. console.log(key, keyPath)
  79. }
  80. onMounted(() => {
  81. console.log(router.options.routes)
  82. })
  83. </script>
  84. <style>
  85. .el-menu-vertical-demo:not(.el-menu--collapse) {
  86. width: 200px;
  87. min-height: 400px;
  88. }
  89. </style>
  90. <style lang="stylus" scoped>
  91. .el-menu
  92. height: 100vh
  93. border-right: none
  94. :deep(.el-sub-menu__title)
  95. height: 50px !important
  96. </style>

路由如下

  1. export const menuRoutes: Array<RouteRecordRaw> = [
  2. {
  3. path: '/adminHome',
  4. name: 'adminHome',
  5. meta: {
  6. title: '常用功能',
  7. icon: 'location'
  8. },
  9. component: () => import('@/views/adminHome.vue'),
  10. children: [
  11. {
  12. path: '/debounce',
  13. name: 'debounce',
  14. meta: {
  15. title: '防抖',
  16. icon: 'location'
  17. },
  18. component: () => import('@/views/adminsystem/debounce/index.vue')
  19. },
  20. {
  21. path: '/throttle',
  22. name: 'throttle',
  23. meta: {
  24. title: '节流',
  25. icon: 'location'
  26. },
  27. component: () => import('@/views/adminsystem/throttle/index.vue')
  28. }
  29. ]
  30. }
  31. ]

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/113126
推荐阅读
相关标签
  

闽ICP备14008679号