Commit 624b450a by Tang

feat: 解决菜单高亮问题

parent 52139722
...@@ -64,7 +64,7 @@ export default { ...@@ -64,7 +64,7 @@ export default {
} }
return (<div class="gs-dropdown-rect"> return (<div class="gs-dropdown-rect">
<div class="rect-title" <div class="rect-title"
vOn:click_stop_prevent={() => { vOn:click={() => {
this.$emit('itemClick', {...nav, root: {...this.nav}}) this.$emit('itemClick', {...nav, root: {...this.nav}})
}} }}
> >
...@@ -82,7 +82,7 @@ export default { ...@@ -82,7 +82,7 @@ export default {
return (<div class={classNames.join(' ').trim()}> return (<div class={classNames.join(' ').trim()}>
{items.map(item => { {items.map(item => {
return (<div class="gs-dropdown-item"> return (<div class="gs-dropdown-item">
<span vOn:click_stop_prevent={() => { <span vOn:click={() => {
this.$emit('itemClick', {...item, root: {...this.nav}}) this.$emit('itemClick', {...item, root: {...this.nav}})
}} }}
class="pointer gs-hover" class="pointer gs-hover"
......
...@@ -5,9 +5,9 @@ ...@@ -5,9 +5,9 @@
<!-- Default Logo --> <!-- Default Logo -->
<a class="navbar-brand" href="/Home" aria-label="Front"> <a class="navbar-brand" href="/Home" aria-label="Front">
<img <img
class="navbar-brand-logo" class="navbar-brand-logo"
src="../../../assets/image/head/logo.png" src="../../../assets/image/head/logo.png"
alt="Logo" alt="Logo"
/> />
</a> </a>
<!-- End Default Logo --> <!-- End Default Logo -->
...@@ -19,37 +19,37 @@ ...@@ -19,37 +19,37 @@
<!-- Nav items--> <!-- Nav items-->
<template v-for="nav in navList"> <template v-for="nav in navList">
<li <li
v-if="nav.children && nav.children.length > 0" v-if="nav.children && nav.children.length > 0"
class="gs-nav-item nav-item" class="gs-nav-item nav-item"
:key="nav.id" :key="nav.id"
:class="{ active: currentNav === nav.id }" :class="{ active: currentNav === nav.id }"
@click.stop="handleNavClick(nav)" @click="handleNavClick(nav,$event)"
> >
<a <a
:id="`gs-nav-item-${nav.id}`" :id="`gs-nav-item-${nav.id}`"
class="nav-link" class="nav-link"
aria-current="page" aria-current="page"
href="javascript:void(0)" href="javascript:void(0)"
role="button" role="button"
data-bs-toggle="dropdown" data-bs-toggle="dropdown"
aria-expanded="false" aria-expanded="false"
>{{ nav.name }}</a >{{ nav.name }}</a
> >
<!-- Dropdown Menu --> <!-- Dropdown Menu -->
<HeaderDropdown :nav="nav" @itemClick="handleItemClick" /> <HeaderDropdown :nav="nav" @itemClick="handleItemClick"/>
<!-- End Dropdown Menu --> <!-- End Dropdown Menu -->
</li> </li>
<li <li
v-else v-else
:key="nav.id" :key="nav.id"
@click.stop="handleNavClick(nav)" @click="handleNavClick(nav,$event)"
class="gs-nav-item nav-item" class="gs-nav-item nav-item"
:class="{ active: currentNav === nav.id }" :class="{ active: currentNav === nav.id }"
> >
<a class="nav-link" href="javascript:void(0)">{{ <a class="nav-link" href="javascript:void(0)">{{
nav.name nav.name
}}</a> }}</a>
</li> </li>
</template> </template>
...@@ -60,7 +60,7 @@ ...@@ -60,7 +60,7 @@
<!-- End Collapse --> <!-- End Collapse -->
<!-- Button --> <!-- Button -->
<HeaderBtn /> <HeaderBtn/>
<!-- End Button --> <!-- End Button -->
</nav> </nav>
</div> </div>
...@@ -70,12 +70,16 @@ ...@@ -70,12 +70,16 @@
<script> <script>
import HeaderBtn from "./HeaderBtn.vue"; import HeaderBtn from "./HeaderBtn.vue";
import HeaderDropdown from "./HeaderDropdown.vue"; import HeaderDropdown from "./HeaderDropdown.vue";
import { nav } from "./mock"; import {nav} from "./mock";
export default { export default {
components: { HeaderBtn, HeaderDropdown }, components: {HeaderBtn, HeaderDropdown},
data() { data() {
return { return {
// 点击事件来自头部导航
fromNav: false,
// 点击事件来自dropdown
fromDropdown: false,
navList: nav, navList: nav,
currentNav: "0", currentNav: "0",
preActive: "", preActive: "",
...@@ -91,14 +95,14 @@ export default { ...@@ -91,14 +95,14 @@ export default {
// this.$store.commit("system/SET_SUB_NAV", nav_.children); // this.$store.commit("system/SET_SUB_NAV", nav_.children);
if (nav_.link === this.$route.path) { if (nav_.link === this.$route.path) {
nav_.breadcrumb && nav_.breadcrumb &&
this.$store.commit("system/SET_BREADCRUMB", nav_.breadcrumb); this.$store.commit("system/SET_BREADCRUMB", nav_.breadcrumb);
} else { } else {
// TODO 去dropdown里面精确查找当前的路径 // TODO 去dropdown里面精确查找当前的路径
const result = this.comparePath(nav_.children, this.$route.path); const result = this.comparePath(nav_.children, this.$route.path);
console.log(result); console.log(result);
result && result &&
result.breadcrumb && result.breadcrumb &&
this.$store.commit("system/SET_BREADCRUMB", result.breadcrumb); this.$store.commit("system/SET_BREADCRUMB", result.breadcrumb);
} }
} }
}); });
...@@ -127,37 +131,59 @@ export default { ...@@ -127,37 +131,59 @@ export default {
return result; return result;
}, },
handleWindowClick() { handleWindowClick() {
console.log(this.preActive); console.log('handleWindowClick', this.fromNav, this.preActive);
if (this.preActive) { if (this.preActive && !this.fromNav) {
this.currentNav = this.preActive; this.currentNav = this.preActive;
this.preActive = ""; this.preActive = "";
} }
this.fromNav = false;
this.fromDropdown = false;
}, },
handleNavClick(nav) { handleNavClick(nav) {
if (!nav.children || nav.children.length === 0) { if (!nav.children || nav.children.length === 0) {
this.currentNav = nav.id; this.currentNav = nav.id;
this.$router.push({ path: nav.link }); this.preActive = "";
this.$router.push({path: nav.link});
} else { } else {
// 点击的是有dropdown的nav 需要保存当前激活状态 // 点击事件不来自子代导航item
this.preActive = this.currentNav; if (!this.fromDropdown) {
this.currentNav = ""; console.log('!this.fromDropdown', this.currentNav, nav.id)
// 重复点击同一个nav
if (this.currentNav === nav.id) {
this.currentNav = this.preActive;
this.preActive = ""
}else{
// 点击的是有dropdown的nav 需要保存当前激活状态
if (this.preActive === "") {
this.preActive = this.currentNav;
}
this.currentNav = nav.id;
this.fromNav = true;
}
}
} }
}, },
handleItemClick(item) { handleItemClick(item) {
this.fromDropdown = true;
if (item.link.startsWith("http")) { if (item.link.startsWith("http")) {
window.open(item.link); window.open(item.link);
} else { } else {
if (this.preActive !== "") { console.log(item.root)
this.preActive = ""; this.updateCurrent(item.root.id);
}
this.currentNav = item.root.id;
item.breadcrumb && item.breadcrumb &&
this.$store.commit("system/SET_BREADCRUMB", item.breadcrumb); this.$store.commit("system/SET_BREADCRUMB", item.breadcrumb);
this.$store.commit("system/SET_SUB_NAV", item.root.children); // this.$store.commit("system/SET_SUB_NAV", item.root.children);
this.$router.push({path: item.link});
this.$router.push({ path: item.link });
} }
}, },
updateCurrent(id) {
console.log('updateCurrent', id);
this.currentNav = id;
this.preActive = "";
}
}, },
}; };
</script> </script>
...@@ -186,6 +212,7 @@ export default { ...@@ -186,6 +212,7 @@ export default {
&.active > a, &.active > a,
a.show { a.show {
color: #f26335; color: #f26335;
&::after { &::after {
content: ""; content: "";
position: absolute; position: absolute;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment