vertical.vue 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <!--
  2. * @Author: vera_min vera_min@163.com
  3. * @Date: 2025-08-02 12:21:54
  4. * @LastEditors: vera_min vera_min@163.com
  5. * @LastEditTime: 2025-08-05 11:15:25
  6. * @FilePath: /sagoo-admin-ui/src/layout/navMenu/vertical.vue
  7. * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
  8. -->
  9. <template>
  10. <el-menu router :default-active="defaultActive" background-color="transparent" :collapse="isCollapse" :unique-opened="getThemeConfig.isUniqueOpened" :collapse-transition="false">
  11. <template v-for="val in menuLists">
  12. <el-sub-menu :index="val.path" v-if="val.children && val.children.length > 0" :key="val.path">
  13. <template #title>
  14. <SvgIcon :name="val.meta?.icon" />
  15. <span>{{ val.name.startsWith('message.') > 0 ? $t(val.name) : $t(val.meta?.title) }}</span>
  16. </template>
  17. <SubItem :chil="val.children" />
  18. </el-sub-menu>
  19. <template v-else>
  20. <template v-if="!val.meta?.isLink || (val.meta?.isLink && val.meta.isIframe)">
  21. <el-menu-item :index="val.path" :key="val.path">
  22. <SvgIcon :name="val.meta?.icon" />
  23. <template #title>
  24. <span>{{ val.name.startsWith('message.') > 0 ? $t(val.name) : $t(val.meta?.title) }}</span>
  25. </template>
  26. </el-menu-item>
  27. </template>
  28. <template v-else>
  29. <el-menu-item :key="val.path">
  30. <SvgIcon :name="val.meta?.icon" />
  31. <template #title>
  32. <a :href="val.meta?.linkUrl" target="_blank" rel="opener" class="w100">{{ val.meta?.name?.startsWith('message.') > 0 ? $t(val.meta?.name) : $t(val.meta?.title) }}</a>
  33. </template>
  34. </el-menu-item>
  35. </template>
  36. </template>
  37. </template>
  38. </el-menu>
  39. </template>
  40. <script lang="ts">
  41. import { toRefs, reactive, computed, defineComponent, onMounted, watch } from 'vue';
  42. import { useRoute, onBeforeRouteUpdate } from 'vue-router';
  43. import { useStore } from '/@/store/index';
  44. import SubItem from '/@/layout/navMenu/subItem.vue';
  45. export default defineComponent({
  46. name: 'navMenuVertical',
  47. components: { SubItem },
  48. props: {
  49. menuList: {
  50. type: Array,
  51. default: () => [],
  52. },
  53. },
  54. setup(props) {
  55. const store = useStore();
  56. const route = useRoute();
  57. const state = reactive({
  58. // 修复:https://gitee.com/lyt-top/vue-next-admin/issues/I3YX6G
  59. defaultActive: route.meta.isDynamic ? route.meta.isDynamicPath : route.path,
  60. isCollapse: false,
  61. });
  62. // 获取父级菜单数据
  63. const menuLists = computed(() => {
  64. return <any>props.menuList;
  65. });
  66. // 获取布局配置信息
  67. const getThemeConfig = computed(() => {
  68. return store.state.themeConfig.themeConfig;
  69. });
  70. // 菜单高亮(详情时,父级高亮)
  71. const setParentHighlight = (currentRoute: any) => {
  72. const { path, meta } = currentRoute;
  73. const pathSplit = meta.isDynamic ? meta.isDynamicPath.split('/') : path.split('/');
  74. if (pathSplit.length >= 4 && meta?.isHide) return pathSplit.splice(0, 3).join('/');
  75. else return path;
  76. };
  77. // 设置菜单的收起/展开
  78. watch(
  79. store.state.themeConfig.themeConfig,
  80. () => {
  81. document.body.clientWidth <= 1000 ? (state.isCollapse = false) : (state.isCollapse = getThemeConfig.value.isCollapse);
  82. },
  83. {
  84. immediate: true,
  85. }
  86. );
  87. // 页面加载时
  88. onMounted(() => {
  89. state.defaultActive = setParentHighlight(route);
  90. });
  91. // 路由更新时
  92. onBeforeRouteUpdate((to) => {
  93. // 修复:https://gitee.com/lyt-top/vue-next-admin/issues/I3YX6G
  94. state.defaultActive = setParentHighlight(to);
  95. const clientWidth = document.body.clientWidth;
  96. if (clientWidth < 1000) getThemeConfig.value.isCollapse = false;
  97. });
  98. return {
  99. menuLists,
  100. getThemeConfig,
  101. ...toRefs(state),
  102. };
  103. },
  104. });
  105. </script>