sidebarItem.vue 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <script setup lang="ts">
  2. import { type RouteRecordRaw } from 'vue-router'
  3. // 做类型限制,解决ts类型报错
  4. type CustomRouteRecordRaw = RouteRecordRaw & {
  5. meta: {
  6. isShow?: boolean
  7. }
  8. }
  9. const props = defineProps({
  10. // 拿到父组件传递过来的路由列表进行渲染
  11. routerList: {
  12. type: Array as () => CustomRouteRecordRaw[],
  13. required: true
  14. }
  15. })
  16. </script>
  17. <template>
  18. <template v-for="item in props.routerList" :key="item.path">
  19. <!-- 当该菜单项有子菜单时 -->
  20. <el-sub-menu
  21. v-if="item.children && item.children.length > 1"
  22. :index="item.path"
  23. >
  24. <template #title v-if="item.meta.icon">
  25. <!-- 菜单项名称,在路由中定义好 -->
  26. <component :is="item.meta.icon" class="el-icon" />
  27. <span>{{ item.meta.title }}</span>
  28. </template>
  29. <!-- 若路由中未定义菜单项icon,则仅展示名称 -->
  30. <template #title v-else>{{ item.meta.title }}</template>
  31. <!-- 递归遍历(核心代码) -->
  32. <sidebarItem :routerList="item.children as CustomRouteRecordRaw[]" />
  33. </el-sub-menu>
  34. <!-- 当前菜单项无子菜单 -->
  35. <el-menu-item :index="item.path" v-else-if="!item.meta.hidden">
  36. <template v-if="item.meta.icon">
  37. <component :is="item.meta.icon" class="el-icon" />
  38. <span>{{ item.meta.title }}</span>
  39. </template>
  40. <template v-else>
  41. {{ item.meta.title }}
  42. </template>
  43. </el-menu-item>
  44. </template>
  45. </template>
  46. <style scoped lang="scss">
  47. .is-active {
  48. background: #409eff;
  49. }
  50. .el-menu-item {
  51. &:hover {
  52. color: #fff;
  53. }
  54. }
  55. .el-menu--collapse {
  56. .el-menu-item {
  57. justify-content: center;
  58. }
  59. }
  60. // 下列代码是用于兼容horizontal所写,酌情删或留
  61. .el-menu--horizontal {
  62. .el-menu-item.is-active {
  63. background-color: transparent !important;
  64. border-bottom: 2px solid #409eff !important;
  65. .el-icon,
  66. span {
  67. color: #409eff !important;
  68. }
  69. }
  70. .el-sub-menu.is-active {
  71. .el-sub-menu__title {
  72. border: 0 !important;
  73. }
  74. .el-icon {
  75. width: 1em;
  76. margin-right: 12px;
  77. font-size: 18px;
  78. }
  79. span {
  80. color: #409eff !important;
  81. }
  82. }
  83. }
  84. </style>