horizontal.vue 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <template>
  2. <div class="el-menu-horizontal-warp">
  3. <el-scrollbar @wheel.native.prevent="onElMenuHorizontalScroll" ref="elMenuHorizontalScrollRef">
  4. <el-menu router :default-active="defaultActive" background-color="transparent" mode="horizontal">
  5. <template v-for="val in menuLists">
  6. <el-sub-menu :index="val.path" v-if="val.children && val.children.length > 0" :key="val.path">
  7. <template #title>
  8. <SvgIcon :name="val.meta?.icon" />
  9. <span>{{ val.meta?.title.indexOf('.')>0?$t(val.meta?.title):val.meta?.title }}</span>
  10. </template>
  11. <SubItem :chil="val.children" />
  12. </el-sub-menu>
  13. <template v-else>
  14. <el-menu-item :index="val.path" :key="val.path">
  15. <template #title v-if="!val.meta?.isLink || (val.meta?.isLink && val.meta.isIframe)">
  16. <SvgIcon :name="val.meta?.icon" />
  17. {{ val.meta?.title.indexOf('.')>0?$t(val.meta?.title):val.meta?.title }}
  18. </template>
  19. <template #title v-else>
  20. <a :href="val.meta?.isLink" target="_blank" rel="opener" class="w100">
  21. <SvgIcon :name="val.meta?.icon" />
  22. {{ val.meta?.title.indexOf('.')>0?$t(val.meta?.title):val.meta?.title }}
  23. </a>
  24. </template>
  25. </el-menu-item>
  26. </template>
  27. </template>
  28. </el-menu>
  29. </el-scrollbar>
  30. </div>
  31. </template>
  32. <script lang="ts">
  33. import { toRefs, reactive, computed, defineComponent, getCurrentInstance, onMounted, nextTick, onBeforeMount } from 'vue';
  34. import { useRoute, onBeforeRouteUpdate } from 'vue-router';
  35. import { useStore } from '/@/store/index';
  36. import SubItem from '/@/layout/navMenu/subItem.vue';
  37. export default defineComponent({
  38. name: 'navMenuHorizontal',
  39. components: { SubItem },
  40. props: {
  41. menuList: {
  42. type: Array,
  43. default: () => [],
  44. },
  45. },
  46. setup(props) {
  47. const { proxy } = <any>getCurrentInstance();
  48. const route = useRoute();
  49. const store = useStore();
  50. const state = reactive({
  51. defaultActive: null,
  52. });
  53. // 获取父级菜单数据
  54. const menuLists = computed(() => {
  55. return <any>props.menuList;
  56. });
  57. // 设置横向滚动条可以鼠标滚轮滚动
  58. const onElMenuHorizontalScroll = (e: any) => {
  59. const eventDelta = e.wheelDelta || -e.deltaY * 40;
  60. proxy.$refs.elMenuHorizontalScrollRef.$refs.wrap$.scrollLeft = proxy.$refs.elMenuHorizontalScrollRef.$refs.wrap$.scrollLeft + eventDelta / 4;
  61. };
  62. // 初始化数据,页面刷新时,滚动条滚动到对应位置
  63. const initElMenuOffsetLeft = () => {
  64. nextTick(() => {
  65. let els: any = document.querySelector('.el-menu.el-menu--horizontal li.is-active');
  66. if (!els) return false;
  67. proxy.$refs.elMenuHorizontalScrollRef.$refs.wrap$.scrollLeft = els.offsetLeft;
  68. });
  69. };
  70. // 路由过滤递归函数
  71. const filterRoutesFun = (arr: Array<object>) => {
  72. return arr
  73. .filter((item: any) => !item.meta.isHide)
  74. .map((item: any) => {
  75. item = Object.assign({}, item);
  76. if (item.children) item.children = filterRoutesFun(item.children);
  77. return item;
  78. });
  79. };
  80. // 传送当前子级数据到菜单中
  81. const setSendClassicChildren = (path: string) => {
  82. const currentPathSplit = path.split('/');
  83. let currentData: any = {};
  84. filterRoutesFun(store.state.routesList.routesList).map((v, k) => {
  85. if (v.path === `/${currentPathSplit[1]}`) {
  86. v['k'] = k;
  87. currentData['item'] = [{ ...v }];
  88. currentData['children'] = [{ ...v }];
  89. if (v.children) currentData['children'] = v.children;
  90. }
  91. });
  92. return currentData;
  93. };
  94. // 设置页面当前路由高亮
  95. const setCurrentRouterHighlight = (currentRoute: any) => {
  96. const { path, meta } = currentRoute;
  97. if (store.state.themeConfig.themeConfig.layout === 'classic') {
  98. (<any>state.defaultActive) = `/${path.split('/')[1]}`;
  99. } else {
  100. const pathSplit = meta.isDynamic ? meta.isDynamicPath.split('/') : path.split('/');
  101. if (pathSplit.length >= 4 && meta.isHide) state.defaultActive = pathSplit.splice(0, 3).join('/');
  102. else state.defaultActive = path;
  103. }
  104. };
  105. // 页面加载前
  106. onBeforeMount(() => {
  107. setCurrentRouterHighlight(route);
  108. });
  109. // 页面加载时
  110. onMounted(() => {
  111. initElMenuOffsetLeft();
  112. });
  113. // 路由更新时
  114. onBeforeRouteUpdate((to) => {
  115. // 修复:https://gitee.com/lyt-top/vue-next-admin/issues/I3YX6G
  116. setCurrentRouterHighlight(to);
  117. // 修复经典布局开启切割菜单时,点击tagsView后左侧导航菜单数据不变的问题
  118. let { layout, isClassicSplitMenu } = store.state.themeConfig.themeConfig;
  119. if (layout === 'classic' && isClassicSplitMenu) {
  120. proxy.mittBus.emit('setSendClassicChildren', setSendClassicChildren(to.path));
  121. }
  122. });
  123. return {
  124. menuLists,
  125. onElMenuHorizontalScroll,
  126. ...toRefs(state),
  127. };
  128. },
  129. });
  130. </script>
  131. <style scoped lang="scss">
  132. .el-menu-horizontal-warp {
  133. flex: 1;
  134. overflow: hidden;
  135. margin-right: 30px;
  136. :deep(.el-scrollbar__bar.is-vertical) {
  137. display: none;
  138. }
  139. :deep(a) {
  140. width: 100%;
  141. }
  142. .el-menu.el-menu--horizontal {
  143. display: flex;
  144. height: 100%;
  145. width: 100%;
  146. box-sizing: border-box;
  147. }
  148. }
  149. </style>