新增:根据当前路由自动展开菜单

This commit is contained in:
chenghx
2018-08-02 17:59:42 +08:00
parent 0aaf3cf893
commit f5b569f170
6 changed files with 114 additions and 5 deletions

View File

@@ -53,9 +53,13 @@ export default {
return {
rootSubmenuKeys: ['/form', '/list', '/detail', '/exception', '/result'],
openKeys: [],
selectedKeys: [],
cachedOpenKeys: []
}
},
created () {
this.updateMenu()
},
watch: {
collapsed (val) {
if (val) {
@@ -64,6 +68,9 @@ export default {
} else {
this.openKeys = this.cachedOpenKeys
}
},
'$route': function () {
this.updateMenu()
}
},
methods: {
@@ -133,6 +140,15 @@ export default {
} else {
this.openKeys = latestOpenKey ? [latestOpenKey] : []
}
},
updateMenu () {
let routes = this.$route.matched.concat()
this.selectedKeys = [routes.pop().path]
let openKeys = []
routes.forEach((item) => {
openKeys.push(item.path)
})
this.openKeys = openKeys
}
},
render (h) {
@@ -143,10 +159,12 @@ export default {
theme: this.$props.theme,
mode: this.$props.mode,
inlineCollapsed: false,
openKeys: this.openKeys
openKeys: this.openKeys,
selectedKeys: this.selectedKeys
},
on: {
openChange: this.onOpenChange
openChange: this.onOpenChange,
select: (obj) => { this.selectedKeys = obj.selectedKeys }
}
}, this.renderMenu(h, this.menuData)
)