vertical.vue 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <template>
  2. <el-menu
  3. router
  4. :default-active="state.defaultActive"
  5. background-color="transparent"
  6. :collapse="state.isCollapse"
  7. :unique-opened="getThemeConfig.isUniqueOpened"
  8. :collapse-transition="false"
  9. >
  10. <template v-for="val in menuLists">
  11. <el-sub-menu :index="val.path" v-if="val.children && val.children.length > 0" :key="val.path">
  12. <template #title>
  13. <navIcon v-if="val.path == '/count/churn'" :val=val style="margin-right: 8px;" />
  14. <navIcon v-else-if="val.path == '/count/engagement'" :val=val style="margin-right: 8px;" />
  15. <navIcon v-else-if="val.path == '/count/device'" :val=val style="margin-right: 8px;" />
  16. <SvgIcon v-else :name="val.meta.icon" />
  17. <span>{{ other.setMenuI18n(val) }}</span>
  18. </template>
  19. <SubItem :chil="val.children" />
  20. </el-sub-menu>
  21. <template v-else>
  22. <el-menu-item :index="val.path" :key="val.path">
  23. <SvgIcon :name="val.meta.icon" />
  24. <template #title v-if="!val.meta.isLink || (val.meta.isLink && val.meta.isIframe)">
  25. <span>{{ other.setMenuI18n(val) }} </span>
  26. </template>
  27. <template #title v-else>
  28. <a class="w100" @click.prevent="onALinkClick(val)">{{ other.setMenuI18n(val) }}</a>
  29. </template>
  30. </el-menu-item>
  31. </template>
  32. </template>
  33. </el-menu>
  34. </template>
  35. <script setup lang="ts" name="navMenuVertical">
  36. import { RouteRecordRaw } from 'vue-router';
  37. import { useThemeConfig } from '/@/stores/themeConfig';
  38. const navIcon = defineAsyncComponent(() => import('/@/layout/navMenu/navIcon.vue'));
  39. import other from '/@/utils/other';
  40. // 引入组件
  41. const SubItem = defineAsyncComponent(() => import('/@/layout/navMenu/subItem.vue'));
  42. // 定义父组件传过来的值
  43. const props = defineProps({
  44. // 菜单列表
  45. menuList: {
  46. type: Array<RouteRecordRaw>,
  47. default: () => [],
  48. },
  49. });
  50. // 定义变量内容
  51. const storesThemeConfig = useThemeConfig();
  52. const { themeConfig } = storeToRefs(storesThemeConfig);
  53. const route = useRoute();
  54. const state = reactive({
  55. defaultActive: route.meta.isDynamic ? route.meta.isDynamicPath : route.path,
  56. isCollapse: false,
  57. });
  58. // 获取父级菜单数据
  59. const menuLists = computed(() => {
  60. return <RouteItems>props.menuList;
  61. });
  62. // 获取布局配置信息
  63. const getThemeConfig = computed(() => {
  64. return themeConfig.value;
  65. });
  66. // 菜单高亮(详情时,父级高亮)
  67. const setParentHighlight = (currentRoute: RouteToFrom) => {
  68. const { path, meta } = currentRoute;
  69. const pathSplit = meta?.isDynamic ? meta.isDynamicPath!.split('/') : path!.split('/');
  70. if (pathSplit.length >= 4 && meta?.isHide) return pathSplit.splice(0, 3).join('/');
  71. else return path;
  72. };
  73. // 打开外部链接
  74. const onALinkClick = (val: RouteItem) => {
  75. other.handleOpenLink(val);
  76. };
  77. // 页面加载时
  78. onMounted(() => {
  79. state.defaultActive = setParentHighlight(route);
  80. document.documentElement.style.setProperty('--menu-bar-active-font-color', themeConfig.value.menuBarActiveFontColor);
  81. });
  82. // 路由更新时
  83. onBeforeRouteUpdate((to) => {
  84. state.defaultActive = setParentHighlight(to);
  85. const clientWidth = document.body.clientWidth;
  86. if (clientWidth < 1000) themeConfig.value.isCollapse = false;
  87. document.documentElement.style.setProperty('--menu-bar-active-font-color', themeConfig.value.menuBarActiveFontColor);
  88. });
  89. // 设置菜单的收起/展开
  90. watch(
  91. themeConfig.value,
  92. () => {
  93. document.body.clientWidth <= 1000 ? (state.isCollapse = false) : (state.isCollapse = themeConfig.value.isCollapse);
  94. },
  95. {
  96. immediate: true,
  97. }
  98. );
  99. </script>