Skip to content

Commit 2c9e7a4

Browse files
authored
fix: 多级菜单下不能正确展开菜单 (#755)
* fix: 多级菜单下,不能正确展开菜单 * chore: 切换路由demo * chore: 修改demo路由 * chore: 优化示例
1 parent ac3ff25 commit 2c9e7a4

2 files changed

Lines changed: 34 additions & 15 deletions

File tree

src/layouts/components/SideNav.vue

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
import { difference, remove, union } from 'lodash';
2727
import type { MenuValue } from 'tdesign-vue-next';
2828
import type { PropType } from 'vue';
29-
import { computed, onMounted, ref, watch } from 'vue';
29+
import { computed, onMounted, onUnmounted, ref, watch } from 'vue';
3030
import { useRouter } from 'vue-router';
3131
3232
import AssetLogoFull from '@/assets/assets-logo-full.svg?component';
@@ -79,12 +79,18 @@ const active = computed(() => getActive());
7979
8080
const expanded = ref<MenuValue[]>([]);
8181
82+
const getExpanded = () => {
83+
const path = getActive();
84+
const parts = path.split('/').slice(1);
85+
const result = parts.map((_, index) => `/${parts.slice(0, index + 1).join('/')}`);
86+
87+
expanded.value = menuAutoCollapsed.value ? result : union(result, expanded.value);
88+
};
89+
8290
watch(
8391
() => active.value,
8492
() => {
85-
const path = getActive();
86-
const parentPath = path.substring(0, path.lastIndexOf('/'));
87-
expanded.value = menuAutoCollapsed.value ? [parentPath] : union([parentPath], expanded.value);
93+
getExpanded();
8894
},
8995
);
9096
@@ -144,13 +150,14 @@ const autoCollapsed = () => {
144150
};
145151
146152
onMounted(() => {
147-
const path = getActive();
148-
const parentPath = path.substring(0, path.lastIndexOf('/'));
149-
expanded.value = union([parentPath], expanded.value);
153+
getExpanded();
150154
autoCollapsed();
151-
window.onresize = () => {
152-
autoCollapsed();
153-
};
155+
156+
window.addEventListener('resize', autoCollapsed);
157+
});
158+
159+
onUnmounted(() => {
160+
window.removeEventListener('resize', autoCollapsed);
154161
});
155162
156163
const goHome = () => {

src/router/modules/homepage.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,27 @@ export default [
3030
},
3131
},
3232
{
33-
path: 'detail',
34-
name: 'DashboardDetail',
35-
component: () => import('@/pages/dashboard/detail/index.vue'),
33+
path: 'dashboard',
34+
name: 'Dashboard',
3635
meta: {
3736
title: {
38-
zh_CN: '统计报表',
39-
en_US: 'Dashboard Detail',
37+
zh_CN: '报表',
38+
en_US: 'Dashboard',
4039
},
4140
},
41+
children: [
42+
{
43+
path: 'detail',
44+
name: 'Detail',
45+
component: () => import('@/pages/dashboard/detail/index.vue'),
46+
meta: {
47+
title: {
48+
zh_CN: '统计报表',
49+
en_US: 'Dashboard Detail',
50+
},
51+
},
52+
},
53+
],
4254
},
4355
],
4456
},

0 commit comments

Comments
 (0)