两种方式
<el-menu :default-active="$route.path"></el-menu>
2.(1)、设置属性 :default-active=“activeIndex” (2)、watch方法检测路由变化 (3)、created生命周期设置路由
<template>
<div style="position: relative">
<el-menu
router
mode="horizontal"
background-color="#545c64"
text-color="#fff"
active-text-color="#ffd04b"
:default-active="activeIndex"
>
<NavMenu :navMenus="menuData"></NavMenu>
</el-menu>
<Dropdown class="drop-content"></Dropdown>
<router-view></router-view>
</div>
</template>
<script>
import NavMenu from "./components/navMenu/navMenu.vue";
import Dropdown from "./components/Languages/index.vue";
export default {
components: {
NavMenu,
Dropdown,
},
data() {
return {
activeIndex: "/",
menuData: [
{
entity: {
id: 0,
index: "",
name: "",
alias: this.$root.$t("menu.rack"),
},
},
{
entity: {
id: 1,
index: "/materialControl",
name: "materialControl",
alias: this.$root.$t("menu.control"),
value: "/material_control/index",
},
},
{
entity: {
id: 6,
index: "/materialTest",
name: "materialTest",
alias: this.$root.$t("menu.test"),
value: "/material_test/index",
},
},
{
entity: {
id: 40,
index: "/materialConfig",
name: "materialConfig",
alias: this.$root.$t("menu.maRConfig"),
value: "/material_config/index",
},
},
{
entity: {
id: 41,
index: "/materialAdmin",
name: "materialAdmin",
alias: this.$root.$t("menu.materialAdmin"),
value: "/material_admin/index",
},
},
],
};
},
watch: {
$route() {
this.setCurrentRoute();
},
},
created() {
this.setCurrentRoute();
},
methods: {
setCurrentRoute() {
this.activeIndex = this.$route.path;
},
},
};
</script>
NavMenu 中的代码:
<template>
<div class="navMenu">
<template v-for="navMenu in navMenus">
<el-menu-item
v-if="!navMenu.childs && navMenu.entity"
:key="navMenu.entity.id"
:data="navMenu"
:index="navMenu.entity.index"
>
<i :class="navMenu.entity.icon"></i>
<span slot="title">{{ navMenu.entity.alias }}</span>
</el-menu-item>
<el-submenu
@click.native="goto(navMenu.entity.value)"
v-if="navMenu.childs && navMenu.entity"
:key="navMenu.entity.id"
:data="navMenu"
:index="navMenu.entity.name"
>
<template slot="title">
<i :class="navMenu.entity.icon"></i>
<span> {{ navMenu.entity.alias }}</span>
</template>
<NavMenu :navMenus="navMenu.childs"></NavMenu>
</el-submenu>
</template>
</div>
</template>
<script>
export default {
name: "NavMenu",
props: ["navMenus"],
data() {
return {};
},
methods: {
goto(path) {
if (path) {
this.$router.push(path);
}
},
},
};
</script>
参考文章:https://blog.csdn.net/nongcunqq/article/details/108715672
|