SiderBar.vue 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <template>
  2. <el-aside class="siderbar_container" :width="isCollapse ? '72px' : '190px'">
  3. <!-- <div @click="toggleCollapse" class="collapse_box">
  4. <img v-if="isCollapse" src="@/assets/icon/open.svg" alt="">
  5. <img v-else src="@/assets/icon/close.svg" alt="">
  6. </div> -->
  7. <el-menu :key="activeRoute" :collapse="isCollapse" router>
  8. <el-menu-item
  9. v-for="item in props.menuItems"
  10. :key="item.route"
  11. :index="item.route"
  12. :class="activeRoute == item.route ? 'is-active' : ''"
  13. >
  14. <template #default>
  15. <i v-if="item.icon" class="iconfont" :class="item.icon"></i>
  16. <component v-if="item.svgIcon" class="img_icon" :is="item.svgIcon" :style="{'--nav-color':item.route == activeRoute ? 'white' : ''}"></component>
  17. <img v-else-if="item.img" class="img_icon" :src="item.route == activeRoute ? item.imgLight : item.img" alt="">
  18. <span v-if="!isCollapse" style="position: relative;" >
  19. {{ item.title }}
  20. <span v-if="item.bubble && item.bubble !== 0" class="bubble">{{item.bubble}} </span>
  21. </span>
  22. </template>
  23. <template #title>
  24. <span v-if="isCollapse">{{ item.title }}</span>
  25. </template>
  26. </el-menu-item>
  27. </el-menu>
  28. </el-aside>
  29. </template>
  30. <script lang="ts">
  31. export default {
  32. name: 'SiderBar'
  33. }
  34. </script>
  35. <script lang="ts" setup>
  36. import { ref, watch, onMounted } from 'vue';
  37. import { useRoute } from 'vue-router'
  38. import router from '@/router';
  39. const route = useRoute()
  40. const activeRoute = ref(route.path)
  41. watch(() => route.path, (newVal) => {
  42. console.log('当前路由---',newVal)
  43. activeRoute.value = newVal
  44. })
  45. const isCollapse = ref(false)
  46. interface menuItem {
  47. title: string;
  48. icon: string;
  49. route: string;
  50. bubble:Number;
  51. img:String;
  52. imgLight:String;
  53. svgIcon:undefined;
  54. }
  55. const props = defineProps({
  56. menuItems:{
  57. required:true,
  58. type:Array<menuItem>
  59. }
  60. })
  61. const toggleCollapse = () => {
  62. isCollapse.value = !isCollapse.value
  63. }
  64. </script>
  65. <style lang="scss" scoped>
  66. .siderbar_container {
  67. // background: #fff;
  68. padding: 20px 0px 20px 20px;
  69. }
  70. .el-menu {
  71. width: 100%;
  72. border: none;
  73. border-radius: 10px;
  74. padding:10px 20px;
  75. }
  76. .el-menu-item {
  77. font-size: 16px;
  78. padding: 0;
  79. }
  80. .iconfont {
  81. margin-right: 5px;
  82. display: inline-block;
  83. color: #666666;
  84. font-size: 16px;
  85. }
  86. .img_icon{
  87. margin-right: 8px;
  88. width: 16px;
  89. height: 16px;
  90. }
  91. .collapse_box {
  92. padding-left: 10px;
  93. // text-align: center;
  94. }
  95. .el-menu.el-menu--vertical :deep(.el-menu-item) {
  96. height: 40px;
  97. margin: 10px 0;
  98. padding: 0;
  99. display: flex;
  100. align-items: center;
  101. justify-content: center;
  102. border-radius: 4px;
  103. .iconfont {
  104. margin-right: 8px;
  105. text-align: center;
  106. }
  107. }
  108. .el-menu--collapse .el-menu-item span {
  109. display: none;
  110. }
  111. .el-menu-item :deep(.el-menu-tooltip__trigger) {
  112. padding: 0 10px;
  113. }
  114. .el-menu-item {
  115. &.is-active,
  116. &.is-active:hover {
  117. font-weight: 600;
  118. background: #2E64FA;
  119. color: #fff;
  120. .iconfont {
  121. color: #fff;
  122. }
  123. }
  124. &:not(.is-active):hover {
  125. background: inherit;
  126. color: #2E64FA;
  127. .img_icon{
  128. --nav-color:#2E64FA;
  129. }
  130. .iconfont {
  131. color: #2E64FA;
  132. }
  133. }
  134. }
  135. .bubble{
  136. display: inline-block;
  137. position: absolute;
  138. top:10px;
  139. line-height: 20px;
  140. width: 20px;
  141. height: 20px;
  142. text-align: center;
  143. font-size: 12px;
  144. border-radius: 10px;
  145. background-color: #F56C6C;
  146. color: white;
  147. }
  148. </style>