ant-design的menu,用this.$router.push到别的页面时,菜单栏没有更新这个路由地址。
点击跳转到别的页面图
页面跳转了,但是menu并没有变化,只有再刷新才会重新获取地址再展开,这个如何解决呢?
changeView(name) {
switch (name) {
case "遥测":
this.$router.replace({ path: "/stationRealTime" });
break;
case "母线":
this.$router.replace({ path: "/busRealTime" });
break;
case "线路":
this.$router.replace({ path: "/lineRealTime" });
break;
case "变压器":
this.$router.push({ path: "/transRealTime" });
break;
case "发电机":
this.$router.push({ path: "/PSrealTime" });
break;
case "无功补偿装置":
this.$router.push({ path: "/statcomRealTime" });
break;
default:
console.log("数据不存在");
}
},
html
<!-- ant的menu -->
<a-menu
theme="dark"
:openKeys="openKeys"
@openChange="onOpenChange"
mode="inline"
:defaultSelectedKeys="defaultSelectedKeys"
>
<template v-for="item in menuList">
<a-menu-item v-if="item.children === false" :key="item.router">
<icon-font :class="item.class" :type="item.icon" />
<router-link class="home-page" :to="item.router" >{{item.name}}</router-link>
</a-menu-item>
<a-sub-menu v-else :key="item.router">
<span slot="title">
<icon-font :class="item.class" :type="item.icon" />
<span>{{item.name}}</span>
</span>
<a-menu-item v-for="menuChildren in item.children" :key="menuChildren.router">
<router-link style="display:block" :to="menuChildren.router">{{menuChildren.name}}</router-link>
</a-menu-item>
</a-sub-menu>
</template>
</a-menu>
<!-- 显示子路由的router-view -->
<a-layout-content>
<router-view
class="home-con"
:default-active="$route.path"
></router-view>
</a-layout-content>
js:
export default {
name: "Home",
data() {
return {
defaultSelectedKeys: [this.$route.path],
openKeys: this.$store.state.openKeys,
menuList,
};
},
components: {
"header-side": headerSide,
IconFont,
},
created() {
const result = this.menuList;
this.menuList = result;
this.showMenu(result);
},
mounted() {
// 监听页面刷新事件
window.addEventListener("unload", this.saveStatus);
},
computed: {
collapsed: {
get: function() {
return this.$store.state.status.collapsed;
},
set: function(newData) {
return newData;
},
activePath() {
return this.$route.path;
},
},
},
methods: {
// 保存侧边栏展开,关闭标识
saveStatus() {
sessionStorage.setItem(
"changeStatus",
JSON.stringify(this.$store.state.status.collapsed)
);
},
// menu事件
showMenu(result) {
// console.log(this.$store.state.openKeys)
for (let i = 0; i < this.menuList.length; i++) {
if (result[i].children !== false) {
for (let y = 0; y < result[i].children.length; y++) {
if (result[i].children[y].router === this.$route.path) {
this.openKeys = [result[i].router]
}
}
}
}
},
onOpenChange(openKeys) {
console.log(this.$store.state.openKeys)
if (openKeys.length !== 0) {
this.openKeys = [openKeys[1]];
} else {
this.openKeys = [""];
}
},
clickItem(obj) {
this.$router.push(obj.key);
},
},
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
antd的menu无法更新选中和展开
home.vue
state.js
mutations.js
15.1. 使用
当前页跳到不同的页面,在即将进入的页面 commit 这两个 mutaion
一个展开项里面把数据传到当前展开项的其他页面