SiderBar.vue 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <template>
  2. <el-aside class="siderbar_container" :width="isCollapse ? '72px' : '150px'">
  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 :collapse="isCollapse" router @handleOpen="handleOpen" @handleClose="handleClose">
  8. <el-menu-item v-for="item in menuItems" :key="item.route" :index="item.route" :class="activeRoute == item.route ? 'is-active' : ''" >
  9. <template #default>
  10. <i class="iconfont" :class="`icon-${item.icon}`"></i>
  11. <span v-if="!isCollapse">{{ item.label }}</span>
  12. </template>
  13. <template #title>
  14. <span v-if="isCollapse">{{ item.label }}</span>
  15. </template>
  16. </el-menu-item>
  17. </el-menu>
  18. </el-aside>
  19. </template>
  20. <script lang="ts">
  21. export default {
  22. name: 'SiderBar'
  23. }
  24. </script>
  25. <script lang="ts" setup>
  26. import { ref, watch } from 'vue';
  27. import { useRoute } from 'vue-router'
  28. const route = useRoute()
  29. const activeRoute = ref(route.path)
  30. watch(() => route.path, (newVal) => {
  31. activeRoute.value = newVal
  32. })
  33. const isCollapse = ref(false)
  34. const menuItems = ref([
  35. {
  36. label: '学校管理',
  37. icon: 'school_normal',
  38. route: '/main/school',
  39. },
  40. {
  41. label: '运维管理',
  42. icon: 'manage_normal',
  43. route: '/main/manager',
  44. },
  45. {
  46. label: '联考管理',
  47. icon: 'school_normal',
  48. route: '/main/examination',
  49. }
  50. ])
  51. const handleOpen = (key, keyPath) => {
  52. console.log(key, keyPath)
  53. }
  54. const handleClose = (key, keyPath) => {
  55. console.log(key, keyPath)
  56. }
  57. const toggleCollapse = () => {
  58. isCollapse.value = !isCollapse.value
  59. }
  60. </script>
  61. <style lang="scss" scoped>
  62. .siderbar_container {
  63. background: #fff;
  64. padding: 16px;
  65. }
  66. .el-menu {
  67. width: 100%;
  68. border: none;
  69. }
  70. .el-menu-item {
  71. padding: 0;
  72. }
  73. .iconfont {
  74. margin-right: 5px;
  75. display: inline-block;
  76. color: #666666;
  77. font-size: 20px;
  78. }
  79. .collapse_box {
  80. padding-left: 10px;
  81. // text-align: center;
  82. }
  83. .el-menu.el-menu--vertical ::v-deep(.el-menu-item) {
  84. height: 40px;
  85. margin: 10px 0;
  86. padding: 0 10px;
  87. border-radius: 4px;
  88. .iconfont {
  89. margin-right: 8px;
  90. text-align: center;
  91. }
  92. }
  93. .el-menu--collapse .el-menu-item span {
  94. display: none;
  95. }
  96. .el-menu-item ::v-deep(.el-menu-tooltip__trigger) {
  97. padding: 0 10px;
  98. }
  99. .el-menu-item {
  100. &.is-active,
  101. &.is-active:hover {
  102. background: #2E64FA;
  103. color: #fff;
  104. .iconfont {
  105. color: #fff;
  106. }
  107. }
  108. &:not(.is-active):hover {
  109. background: inherit;
  110. color: #2E64FA;
  111. .iconfont {
  112. color: #2E64FA;
  113. }
  114. }
  115. }
  116. </style>