feat: add prop openKeys and event openChange for menu.js; 🌟

新增:menu.js 增加 openKeys 属性和 openChange 事件;
master
iczer 4 years ago
parent bfb0358217
commit 08e40f1a31
  1. 66
      src/components/menu/menu.js
  2. 2
      src/utils/colors.js

@ -22,15 +22,18 @@
* } * }
* ] * ]
* *
* i18n: 国际化配置组件默认会根据 options route配置的 path name 生成英文以及中文的国际化配置如需自定义或增加其他语言配置 * i18n: 国际化配置系统默认会根据 options route配置的 path name 生成英文以及中文的国际化配置如需自定义或增加其他语言配置
* 此项即可 * 此项即可
* i18n: { * i18n: {
* CN: {dashboard: {name: '监控中心'}} * messages: {
* HK: {dashboard: {name: '監控中心'}} * CN: {dashboard: {name: '监控中心'}}
* HK: {dashboard: {name: '監控中心'}}
* }
* } * }
**/ **/
import Menu from 'ant-design-vue/es/menu' import Menu from 'ant-design-vue/es/menu'
import Icon from 'ant-design-vue/es/icon' import Icon from 'ant-design-vue/es/icon'
import fastEqual from 'fast-deep-equal'
const {Item, SubMenu} = Menu const {Item, SubMenu} = Menu
@ -56,29 +59,26 @@ export default {
required: false, required: false,
default: false default: false
}, },
i18n: Object i18n: Object,
openKeys: Array
}, },
data () { data () {
return { return {
openKeys: [],
selectedKeys: [], selectedKeys: [],
sOpenKeys: [],
cachedOpenKeys: [] cachedOpenKeys: []
} }
}, },
computed: { computed: {
rootSubmenuKeys: (vm) => {
let keys = []
vm.options.forEach(item => {
keys.push(item.path)
})
return keys
},
menuTheme() { menuTheme() {
return this.theme == 'light' ? this.theme : 'dark' return this.theme == 'light' ? this.theme : 'dark'
} }
}, },
created () { created () {
this.updateMenu() this.updateMenu()
if (!this.options[0].fullPath) {
this.formatOptions(this.options, '')
}
// 自定义国际化配置 // 自定义国际化配置
if(this.i18n && this.i18n.messages) { if(this.i18n && this.i18n.messages) {
const messages = this.i18n.messages const messages = this.i18n.messages
@ -90,14 +90,18 @@ export default {
watch: { watch: {
collapsed (val) { collapsed (val) {
if (val) { if (val) {
this.cachedOpenKeys = this.openKeys this.cachedOpenKeys = this.sOpenKeys
this.openKeys = [] this.sOpenKeys = []
} else { } else {
this.openKeys = this.cachedOpenKeys this.sOpenKeys = this.cachedOpenKeys
} }
}, },
'$route': function () { '$route': function () {
this.updateMenu() this.updateMenu()
},
sOpenKeys(val) {
this.$emit('openChange', val)
this.$emit('update:openKeys', val)
} }
}, },
methods: { methods: {
@ -159,32 +163,22 @@ export default {
return menuArr return menuArr
}, },
formatOptions(options, parentPath) { formatOptions(options, parentPath) {
let this_ = this
options.forEach(route => { options.forEach(route => {
let isFullPath = route.path.substring(0, 1) == '/' let isFullPath = route.path.substring(0, 1) == '/'
route.fullPath = isFullPath ? route.path : parentPath + '/' + route.path route.fullPath = isFullPath ? route.path : parentPath + '/' + route.path
if (route.children) { if (route.children) {
this_.formatOptions(route.children, route.fullPath) this.formatOptions(route.children, route.fullPath)
} }
}) })
}, },
onOpenChange (openKeys) {
const latestOpenKey = openKeys.find(key => this.openKeys.indexOf(key) === -1)
if (this.rootSubmenuKeys.indexOf(latestOpenKey) === -1) {
this.openKeys = openKeys
} else {
this.openKeys = latestOpenKey ? [latestOpenKey] : []
}
},
updateMenu () { updateMenu () {
let routes = this.$route.matched.concat() const menuRoutes = this.$route.matched.filter(item => item.path !== '')
const route = routes.pop() const route = menuRoutes.pop()
this.selectedKeys = [this.getSelectedKey(route)] this.selectedKeys = [this.getSelectedKey(route)]
let openKeys = [] let openKeys = menuRoutes.map(item => item.path)
routes.forEach((item) => { if (!fastEqual(openKeys, this.sOpenKeys)) {
openKeys.push(item.path) this.collapsed || this.mode === 'horizontal' ? this.cachedOpenKeys = openKeys : this.sOpenKeys = openKeys
}) }
this.collapsed || this.mode === 'horizontal' ? this.cachedOpenKeys = openKeys : this.openKeys = openKeys
}, },
getSelectedKey (route) { getSelectedKey (route) {
if (route.meta.invisible && route.parent) { if (route.meta.invisible && route.parent) {
@ -200,14 +194,16 @@ export default {
props: { props: {
theme: this.menuTheme, theme: this.menuTheme,
mode: this.$props.mode, mode: this.$props.mode,
openKeys: this.openKeys, selectedKeys: this.selectedKeys,
selectedKeys: this.selectedKeys openKeys: this.openKeys ? this.openKeys : this.sOpenKeys
}, },
on: { on: {
openChange: this.onOpenChange,
select: (obj) => { select: (obj) => {
this.selectedKeys = obj.selectedKeys this.selectedKeys = obj.selectedKeys
this.$emit('select', obj) this.$emit('select', obj)
},
'update:openKeys': (val) => {
this.sOpenKeys = val
} }
} }
}, this.renderMenu(h, this.options) }, this.renderMenu(h, this.options)

@ -1,5 +1,5 @@
const varyColor = require('webpack-theme-color-replacer/client/varyColor') const varyColor = require('webpack-theme-color-replacer/client/varyColor')
const generate = require('@ant-design/colors').generate const {generate} = require('@ant-design/colors')
const {ADMIN, ANTD} = require('../config/default') const {ADMIN, ANTD} = require('../config/default')
const Config = require('../config') const Config = require('../config')

Loading…
Cancel
Save