Former-commit-id: a028cd2bfb5d361eeb019eafc1203d26fd68a35e
This commit is contained in:
FairyEver
2019-08-11 00:11:13 +08:00
parent 1ae45c5f62
commit ac906425c0
3 changed files with 25 additions and 16 deletions
@@ -1,8 +1,6 @@
import { uniqueId } from 'lodash'
// 创建 el-menu-item // 创建 el-menu-item
export function elMenuItem (createElement, menu) { export function elMenuItem (createElement, menu) {
return createElement('el-menu-item', { props: { index: menu.path || uniqueId('d2-menu-empty-') } }, [ return createElement('el-menu-item', { props: { index: menu.path } }, [
...menu.icon ? [ ...menu.icon ? [
createElement('i', { attrs: { class: `fa fa-${menu.icon}` } }) createElement('i', { attrs: { class: `fa fa-${menu.icon}` } })
] : [], ] : [],
@@ -12,13 +10,13 @@ export function elMenuItem (createElement, menu) {
...menu.iconSvg ? [ ...menu.iconSvg ? [
createElement('d2-icon-svg', { props: { name: menu.iconSvg } }) createElement('d2-icon-svg', { props: { name: menu.iconSvg } })
] : [], ] : [],
createElement('span', { slot: 'title' }, menu.title || '未命名菜单') createElement('span', { slot: 'title' }, menu.title || this.$t('layout.header-aside.menu-item.label-default'))
]) ])
} }
// 创建 el-submenu // 创建 el-submenu
export function elSubmenu (createElement, menu) { export function elSubmenu (createElement, menu) {
return createElement('el-submenu', { props: { index: menu.path || uniqueId('d2-menu-empty-') } }, [ return createElement('el-submenu', { props: { index: menu.path } }, [
...menu.icon ? [ ...menu.icon ? [
createElement('i', { slot: 'title', attrs: { class: `fa fa-${menu.icon}` } }) createElement('i', { slot: 'title', attrs: { class: `fa fa-${menu.icon}` } })
] : [], ] : [],
@@ -28,7 +26,7 @@ export function elSubmenu (createElement, menu) {
...menu.iconSvg ? [ ...menu.iconSvg ? [
createElement('d2-icon-svg', { slot: 'title', props: { name: menu.iconSvg } }) createElement('d2-icon-svg', { slot: 'title', props: { name: menu.iconSvg } })
] : [], ] : [],
createElement('span', { slot: 'title' }, menu.title || '未命名菜单'), createElement('span', { slot: 'title' }, menu.title || this.$t('layout.header-aside.menu-item.label-default')),
...menu.children.map((child, childIndex) => (child.children === undefined ? elMenuItem : elSubmenu).call(this, createElement, child)) ...menu.children.map((child, childIndex) => (child.children === undefined ? elMenuItem : elSubmenu).call(this, createElement, child))
]) ])
} }
@@ -45,14 +45,9 @@ export default {
}, 500) }, 500)
}, },
// 监听路由 控制侧边栏激活状态 // 监听路由 控制侧边栏激活状态
'$route': { '$route.fullPath': {
handler ({ fullPath }) { handler (value) {
this.active = fullPath this.active = value
this.$nextTick(() => {
if (this.aside.length > 0 && this.$refs.menu) {
this.$refs.menu.activeIndex = fullPath
}
})
}, },
immediate: true immediate: true
} }
+18 -2
View File
@@ -1,6 +1,22 @@
import { uniqueId } from 'lodash'
// 设置文件 // 设置文件
import setting from '@/setting.js' import setting from '@/setting.js'
/**
* 给菜单数据补充上 path 字段
* https://github.com/d2-projects/d2-admin/issues/209
* @param {Array} menu 原始的菜单数据
*/
function supplementMenuPath (menu) {
return menu.map(e => ({
...e,
path: e.path || uniqueId('d2-menu-empty-'),
...e.children ? {
children: supplementMenuPath(e.children)
} : {}
}))
}
export default { export default {
namespaced: true, namespaced: true,
state: { state: {
@@ -77,7 +93,7 @@ export default {
*/ */
headerSet (state, menu) { headerSet (state, menu) {
// store 赋值 // store 赋值
state.header = menu state.header = supplementMenuPath(menu)
}, },
/** /**
* @description 设置侧边栏菜单 * @description 设置侧边栏菜单
@@ -86,7 +102,7 @@ export default {
*/ */
asideSet (state, menu) { asideSet (state, menu) {
// store 赋值 // store 赋值
state.aside = menu state.aside = supplementMenuPath(menu)
} }
} }
} }