这个现在都有存储上,默认显示了是第一个menu,但是无法改变menu值。
高亮样式:
我给改变选中menu的key值的函数加了个debugger,结果他显示的是我的其他页面。。。
openKeys: 展开的menu。
selectedKeys:选中的子集menu。
computed代码:
openKeys:{
get: function() {
this.$store.state.keys.openKeys = ['sub1'];
return this.$store.state.keys.openKeys;
},
set: function(newData) {
return newData;
},
},
selectedKeys:{
get: function() {
this.$store.state.keys.selectedKeys = ['1'];
return this.$store.state.keys.selectedKeys;
},
set: function(newData) {
return newData;
},
},
mounted代码:
// 监听页面刷新事件
window.addEventListener("unload", this.saveKeys);
methods代码:
// 保存到seesionStorage中
saveKeys() {
sessionStorage.setItem(
"openKeys",
JSON.stringify(this.$store.state.keys.openKeys)
);
sessionStorage.setItem(
"selectedKeys",
JSON.stringify(this.$store.state.keys.selectedKeys)
);
},
// 改变选中menu的key值
onOpenChange(openKeys) {
const latestOpenKey = openKeys.find(key => this.openKeys.indexOf(key) === -1);
this.$nextTick(() => {
if (this.rootSubmenuKeys.indexOf(latestOpenKey) === -1) {
this.openKeys = openKeys;
} else {
this.openKeys = latestOpenKey ? [latestOpenKey] : [];
}
})
},
vuex-state:
keys: {
openKeys: JSON.parse(sessionStorage.getItem("openKeys")),
selectedKeys: JSON.parse(sessionStorage.getItem("selectedKeys")),
}~~~~
不需要用vuex和storage,使用route也可以直接实现。
html代码:
js代码:
列表数据: