HomeAside.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <script>
  2. import menuList from '@/data/json/menuList.json'
  3. import {requireImg} from "@/utils/requireImg";
  4. export default {
  5. props: {
  6. collapse: Boolean,
  7. },
  8. data() {
  9. return {
  10. selectedKey: [],
  11. iconVerticalAlign: '',
  12. asideStyle: {},
  13. menuList,
  14. }
  15. },
  16. watch: {
  17. collapse(val) {
  18. this.suitHeight(val)
  19. },
  20. $route() {
  21. this.getCurrRoute()
  22. this.$forceUpdate();
  23. }
  24. },
  25. mounted() {
  26. this.suitHeight(this.collapse)
  27. },
  28. emits: ['update:collapse'],
  29. setup(props, context) {
  30. const methods = {
  31. toggleCollapse() {
  32. const val = !props.collapse
  33. context.emit('update:collapse', val)
  34. },
  35. }
  36. return methods
  37. },
  38. methods: {
  39. requireImg,
  40. suitHeight(val) {
  41. this.getCurrRoute()
  42. this.$nextTick(() => {
  43. // let height = document.getElementsByClassName('home')[0].clientHeight
  44. this.asideStyle = { height: `100%` }
  45. if (val)
  46. this.asideStyle.width = '60px'
  47. if (this.collapse) {
  48. document.getElementsByClassName("aside-collapse")[0].style.textAlign = 'center'
  49. document.getElementsByClassName("aside-collapse")[0].style.marginLeft = '0'
  50. this.iconVerticalAlign = '';
  51. } else {
  52. document.getElementsByClassName("aside-collapse")[0].style.textAlign = ''
  53. document.getElementsByClassName("aside-collapse")[0].style.marginLeft = '5%'
  54. this.iconVerticalAlign = 'text-top';
  55. }
  56. })
  57. },
  58. getCurrRoute() {
  59. this.$nextTick(()=>{
  60. if (this.$route.matched.length>1) {
  61. this.selectedKey = [this.$route.matched[1].path+'']
  62. }
  63. let index = this.menuList.findIndex(i=>i.router==this.selectedKey)
  64. this.handleClickMenuItem(this.menuList[index])
  65. this.$forceUpdate()
  66. })
  67. },
  68. handleClickMenuItem(item) {
  69. this.$store.menuStore().currMenu = item;
  70. },
  71. handleSelected(item) {
  72. this.$router.push({path: item.key})
  73. },
  74. },
  75. }
  76. </script>
  77. <template>
  78. <div class="aside">
  79. <a-menu
  80. class="menu"
  81. theme="dark"
  82. :style="asideStyle"
  83. v-model:selectedKeys="selectedKey"
  84. mode="inline"
  85. @select="handleSelected"
  86. >
  87. <a-menu-item-group key="g1">
  88. <template slot="title">
  89. <div style="height: 12px"></div>
  90. <div class="aside-collapse" style="color: white; cursor: pointer;margin-left: 5%" @click="toggleCollapse">
  91. <a-icon style="font-size: 18px;vertical-align: text-top" type="menu-fold" v-if="!collapse" />
  92. <a-icon style="font-size: 18px;vertical-align: text-top;" type="menu-unfold" v-if="collapse" />
  93. <span style="margin-left: 15px;vertical-align: top" v-if="!collapse">展开/收起</span>
  94. </div>
  95. </template>
  96. </a-menu-item-group>
  97. <a-menu-item v-for="item in menuList" :key="item.router" @click="handleClickMenuItem(item)" >
  98. <span class="anticon" :style="{verticalAlign: iconVerticalAlign}">
  99. <a-avatar class="function-avatar" shape="square" :size="20" :src="requireImg(item.icon)"></a-avatar>
  100. </span>
  101. <span >
  102. {{ item.name }}
  103. </span>
  104. </a-menu-item>
  105. </a-menu>
  106. </div>
  107. </template>
  108. <style lang="less" scoped>
  109. @menu-dark-bg: #266a99;
  110. @menu-dark-submenu-bg: #266a99;
  111. @menu-dark-color: #fff;
  112. @menu-dark-arrow-color: #fff;
  113. @menu-dark-highlight-color: #fff;
  114. .aside-collapse {
  115. width: 100%;
  116. }
  117. .aside-collapse:hover {
  118. span {
  119. color: #2ba8e4;
  120. }
  121. i {
  122. color: #2ba8e4;
  123. }
  124. }
  125. .aside {
  126. //width: 100%;
  127. height: 100%;
  128. /*菜单样式*/
  129. /deep/ .ant-menu-sub {
  130. box-shadow: none !important;
  131. }
  132. /deep/ .ant-menu-item-selected {
  133. color: @menu-dark-selected-item-text-color !important;
  134. }
  135. .ant-menu-inline-collapsed > .ant-menu-item {
  136. padding: 0 !important;
  137. margin-left: 0 !important;
  138. padding-left: 33% !important;
  139. }
  140. /deep/ .ant-menu-inline-collapsed > .ant-menu-item-group > .ant-menu-item-group-list > .ant-menu-item {
  141. padding: 0 !important;
  142. margin-left: 0 !important;
  143. padding-left: 33% !important;
  144. }
  145. }
  146. </style>