Skip to content

Commit efc9127

Browse files
committed
fix: 修复 barleft 行为
之前只有首次进入网页会记录当前所在栏目,并让当前栏目在高度不够时保持开启(除非手动关闭),现在切换路由时候会记录新的所在栏目 并在后续保持开启
1 parent bda91ce commit efc9127

File tree

4 files changed

+42
-25
lines changed

4 files changed

+42
-25
lines changed

app.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ onMounted(() => {
3838
window.WebKitMutationObserver ||
3939
window.MozMutationObserver;
4040
const observer = new mutationObserver(() => {
41-
$mitt.emit('routeSwitching');
41+
$mitt.emit('mainDomChange');
4242
});
4343
observer.observe(bodyRef.value, {
4444
childList: true

components/bar/BarLeft.vue

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<script setup>
2-
const { tm, locale } = useI18n();
2+
const { tm, _locale } = useI18n();
33
44
const getComp = computed(() => {
55
const textValue = tm('BarLeft');
@@ -75,6 +75,8 @@ const rowHeight = 32;
7575
const chunkPading = 6;
7676
const rowHeightpx = `${rowHeight}px`;
7777
78+
const thisElSubEnum = ref(undefined);
79+
7880
const route = useRoute();
7981
8082
const openMenu = (MenuOpenEvent) => {
@@ -94,17 +96,8 @@ const openMenu = (MenuOpenEvent) => {
9496
openMenuList.add(MenuOpenEvent);
9597
};
9698
97-
const deleteSetItem = (target, setList) => {
98-
for (const item of setList) {
99-
if (item === target) {
100-
setList.delete(item);
101-
break;
102-
}
103-
}
104-
};
105-
10699
const closeMenu = (MenuOpenEvent) => {
107-
deleteSetItem(MenuOpenEvent, openMenuList);
100+
openMenuList.delete(MenuOpenEvent);
108101
menuRef.value.close(MenuOpenEvent);
109102
};
110103
@@ -123,17 +116,34 @@ const highlyIsQualified = (height) => {
123116
124117
const { $mitt } = useNuxtApp();
125118
onMounted(() => {
126-
$mitt.on('routeSwitching', () => {
127-
nextTick(() => {
128-
retractMenuBar();
129-
});
119+
$mitt.on('mainDomChange', () => {
120+
retractMenuBar();
130121
});
131122
});
132123
onBeforeUnmount(() => {
133-
$mitt.off('routeSwitching');
124+
$mitt.off('mainDomChange');
134125
});
135126
127+
const routeMessage = useRouteMessageStore();
136128
const retractMenuBar = () => {
129+
let thisElSubEnmu_cache = undefined;
130+
if (routeMessage.toPath !== '/') {
131+
for (const [index, item] of navigationList.value.entries()) {
132+
if (
133+
item.children.find((item1) => routeMessage.toPath.includes(item1.url))
134+
) {
135+
// 当前所在不入列尾
136+
thisElSubEnmu_cache = index;
137+
break;
138+
}
139+
}
140+
if (thisElSubEnum.value !== undefined)
141+
openMenuList.add(String(thisElSubEnum.value));
142+
143+
openMenuList.delete(String(thisElSubEnmu_cache));
144+
thisElSubEnum.value = thisElSubEnmu_cache;
145+
}
146+
137147
let height = menuDivRef.value.clientHeight;
138148
for (const item of openMenuList) {
139149
if (highlyIsQualified(height)) {
@@ -145,9 +155,6 @@ const retractMenuBar = () => {
145155
closeMenu(item);
146156
}
147157
}
148-
// for (const item of openMenuList) {
149-
// menuRef.value.open(item);
150-
// }
151158
};
152159
onMounted(() => {
153160
// 每次缩放改变的时候,判断有没有栏目需要缩回去,先展开的,优先缩进
@@ -164,12 +171,11 @@ onMounted(() => {
164171
let height = menuDivRef.value.clientHeight;
165172
// 初次加载的时候尝试打开当前栏目分类
166173
// 记一下目前所在分类的title
167-
let thisElSubEnum = null;
168174
for (const [index, item] of navigationList.value.entries()) {
169-
if (thisElSubEnum === null) {
175+
if (thisElSubEnum.value === undefined) {
170176
if (item.children.find((item1) => route.path.includes(item1.url))) {
171177
// 当前所在不入列尾
172-
thisElSubEnum = index;
178+
thisElSubEnum.value = index;
173179
} else {
174180
openMenuList.add(String(index));
175181
}
@@ -183,7 +189,7 @@ onMounted(() => {
183189
if (highlyIsQualified(height)) {
184190
break;
185191
}
186-
if (thisElSubEnum !== index) {
192+
if (thisElSubEnum.value !== index) {
187193
height = height - (item.children.length * rowHeight + chunkPading);
188194
closeMenu(String(index));
189195
}
@@ -234,6 +240,7 @@ const backToTopBtnShow = ref(false);
234240
:default-openeds="defaultOpeneds"
235241
@close="closeMenu"
236242
@open="openMenu">
243+
<!-- 它的 index 要求是字符串不然控制台发警告 -->
237244
<el-sub-menu
238245
v-for="(item, index) in navigationList"
239246
:key="`barleft-1-link-${index}`"

middleware/routeguard.global.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1-
export default defineNuxtRouteMiddleware((_to, _from) => {
1+
export default defineNuxtRouteMiddleware((to, from) => {
2+
const routeMessage = useRouteMessageStore();
3+
routeMessage.toPath = to.path;
4+
routeMessage.fromPath = from.path;
25
return;
36
});

stores/routeMessage.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { defineStore } from 'pinia';
2+
export const useRouteMessageStore = defineStore('routeMessage', {
3+
state: () => ({
4+
toPath: '',
5+
fromPath: ''
6+
})
7+
});

0 commit comments

Comments
 (0)