当前位置:   article > 正文

element-plus动态渲染el-menu(vue3)_elementplus text-color

elementplus text-color

 样式如上

需要typescript,vue3环境下运行

需要下载依赖如下

  1. npm install sass-loader node-sass --save-dev
  2. npm install element-plus --save
  3. npm install view-ui-plus@1.3.1 --save
  4. main.ts中
  5. import 'view-ui-plus/dist/styles/viewuiplus.css'
  6. import ElementPlus from 'element-plus'
  7. import 'element-plus/dist/index.css'
  8. app.use(ElementPlus)

 需要scss以及,element-plus

  1. <template>
  2. <div class="sidebar">
  3. <el-menu :default-active="activeIndex" class="menu" active-text-color="#FFFFFF" background-color="#060E83"
  4. text-color="#fff" :unique-opened="true" router @select="handleSelect" @close="menuClose" @open="menuOpen">
  5. <template v-for="menus in ItemList" :key="menus">
  6. <el-sub-menu :key="menus.menuId" :index="menus.index" v-if="menus.childs">
  7. <template #title>
  8. <img :src="menus.icon" alt="" style="margin-right: 16px;width: 22px;height: 22px;">
  9. <span>{{ menus.title }}</span>
  10. </template>
  11. <el-timeline style="box-sizing: border-box;padding-left: 20px;">
  12. <el-timeline-item v-for="(childvl, ci) in menus.childs" :key="ci" center @click="changeColor">
  13. <!-- {{ activity.content }} -->
  14. <el-menu-item :index="childvl.index" :key="ci" style="margin-bottom:10px;">{{
  15. childvl.title
  16. }}</el-menu-item>
  17. </el-timeline-item>
  18. </el-timeline>
  19. </el-sub-menu>
  20. <el-menu-item :index="menus.index" v-else style="margin-bottom:10px;">
  21. <img :src="menus.icon" alt="" style="margin-right: 16px;">
  22. <span>{{ menus.title }}</span>
  23. </el-menu-item>
  24. </template>
  25. </el-menu>
  26. </div>
  27. </template>
  28. <script lang="ts" setup>
  29. import getData from "@/assets/json/st_classlist.json";
  30. import { ref, onMounted } from "vue";
  31. const ItemList: any = ref([])
  32. const activeIndex = ref('')
  33. const handleSelect = (key: any) => {
  34. activeIndex.value = key
  35. }
  36. function addRouterIndex() {
  37. setTimeout(() => {
  38. const childrenArray = ItemList.value.map((item: any) => {
  39. return item.childs;
  40. });
  41. //用来存放child里不为null的值
  42. let intChild = []
  43. intChild = childrenArray.filter(function (val: any) {
  44. return val
  45. });
  46. //把intChild中的所有数组拼接起来
  47. const flattenedArray = intChild.reduce((acc: any[], cur: any) => {
  48. return acc.concat(cur);
  49. }, []);
  50. //获取该路由所对应的下标
  51. const index = flattenedArray.findIndex((person: { index: string; }) => person.index === activeIndex.value);
  52. let nodes = document.querySelectorAll('.el-timeline-item__node')[index] as HTMLElement
  53. if (nodes) {
  54. nodes.style.background = "#fff"
  55. nodes.style.transition = "0.5s"
  56. }
  57. }, 100);
  58. }
  59. const popstate = () => {
  60. activeIndex.value = location.hash.split("/")[1]
  61. addRouterIndex()
  62. }
  63. const changeColor = () => {
  64. addRouterIndex()
  65. }
  66. const menuClose = () => {
  67. addRouterIndex()
  68. }
  69. const menuOpen = () => {
  70. addRouterIndex()
  71. }
  72. onMounted(() => {
  73. // console.log(getData.data);
  74. ItemList.value = getData.data
  75. popstate()
  76. window.addEventListener('popstate', popstate, false);
  77. addRouterIndex()
  78. console.log(ItemList.value);
  79. })
  80. </script>
  81. <style scoped lang="scss">
  82. .sidebar {
  83. height: calc(100vh - 76px);
  84. box-shadow: 2px 0px 6px 0px rgba(0, 0, 0, 0.1);
  85. .menu {
  86. border-right: unset;
  87. height: 100%;
  88. width: 240px;
  89. font-size: 16px;
  90. .el-sub-menu {
  91. width: 224px;
  92. height: auto;
  93. border-radius: 8px;
  94. margin: auto;
  95. }
  96. ::v-deep .el-sub-menu__title {
  97. font-size: 16px;
  98. }
  99. .el-menu-item {
  100. // width: 224px;
  101. height: 50px;
  102. border-radius: 8px;
  103. margin: auto;
  104. font-size: 16px;
  105. }
  106. .is-active {
  107. background: rgba(94, 103, 246, 0.3);
  108. }
  109. .logo {
  110. height: 50px;
  111. line-height: 50px;
  112. padding: 0 10px;
  113. overflow: hidden;
  114. img {
  115. vertical-align: middle;
  116. margin-left: 6px;
  117. }
  118. span {
  119. font-weight: bold;
  120. font-size: 16px;
  121. }
  122. }
  123. }
  124. .menu:not(.el-menu--collapse) {
  125. width: 240px;
  126. }
  127. }
  128. ::v-deep .el-sub-menu .el-menu-item {
  129. min-width: 0;
  130. }
  131. ::v-deep .el-timeline-item__wrapper {
  132. top: 0px !important;
  133. height: 49px;
  134. }
  135. ::v-deep .el-timeline-item__node {
  136. background: #2B329A;
  137. }
  138. .el-timeline {
  139. --el-timeline-node-color: #2B329A;
  140. }
  141. .el-active-fff {
  142. background: #fff;
  143. }
  144. ::v-deep .el-timeline-item__wrapper {
  145. height: 40px;
  146. }
  147. .sidebar .menu[data-v-0f7446ba]:not(.el-menu--collapse) {
  148. box-sizing: border-box;
  149. padding-top: 20px;
  150. }
  151. </style>

下面是JSON文件内容

  1. {
  2. "code": 200,
  3. "message": "操作成功",
  4. "data": [
  5. {
  6. "index": "ceshione",
  7. "icon": "https://img.icons8.com/3d-fluency/2x/fingerprint.png%202x,%20https://img.icons8.com/3d-fluency/1x/fingerprint.png",
  8. "title": "测试",
  9. "menuId": "1082",
  10. "component": null,
  11. "childs": [
  12. {
  13. "index": "ceshitwo",
  14. "icon": "#",
  15. "title": "测试",
  16. "menuId": "1087",
  17. "component": null,
  18. "childs": null
  19. },
  20. {
  21. "index": "ceshithree",
  22. "icon": "#",
  23. "title": "测试",
  24. "menuId": "1088",
  25. "component": null,
  26. "childs": null
  27. }
  28. ]
  29. },
  30. {
  31. "index": "ceshifore",
  32. "icon": "https://img.icons8.com/3d-fluency/2x/fingerprint.png%202x,%20https://img.icons8.com/3d-fluency/1x/fingerprint.png",
  33. "title": "测试",
  34. "menuId": "1083",
  35. "component": null,
  36. "childs": [
  37. {
  38. "index": "ceshifive",
  39. "icon": "#",
  40. "title": "测试",
  41. "menuId": "1089",
  42. "component": null,
  43. "childs": null
  44. }
  45. ]
  46. },
  47. {
  48. "index": "",
  49. "icon": "https://img.icons8.com/3d-fluency/2x/fingerprint.png%202x,%20https://img.icons8.com/3d-fluency/1x/fingerprint.png",
  50. "title": "测试",
  51. "menuId": "1084",
  52. "component": null,
  53. "childs": [
  54. {
  55. "index": "ceshisix",
  56. "icon": "#",
  57. "title": "测试",
  58. "menuId": "1001",
  59. "component": null,
  60. "childs": null
  61. }
  62. ]
  63. },
  64. {
  65. "index": "#",
  66. "icon": "https://img.icons8.com/3d-fluency/2x/fingerprint.png%202x,%20https://img.icons8.com/3d-fluency/1x/fingerprint.png",
  67. "title": "测试",
  68. "menuId": "1086",
  69. "component": null,
  70. "childs": [
  71. {
  72. "index": "ceshieight",
  73. "icon": "#",
  74. "title": "测试",
  75. "menuId": "1089",
  76. "component": null,
  77. "childs": null
  78. }
  79. ]
  80. }
  81. ]
  82. }

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

闽ICP备14008679号