From 97a1417112ae647c57685a1973ec5679fa3f7e14 Mon Sep 17 00:00:00 2001 From: chenghongxing <1126263215@qq.com> Date: Thu, 6 May 2021 11:13:35 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=EF=BC=9A=E8=B7=AF=E7=94=B1?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=E5=A2=9E=E5=8A=A0=E5=8A=A8=E6=80=81=E8=B7=AF?= =?UTF-8?q?=E7=94=B1=E5=8F=82=E6=95=B0=E6=94=AF=E6=8C=81=20=E5=92=8C=20?= =?UTF-8?q?=E9=AB=98=E4=BA=AE=E8=8F=9C=E5=8D=95=E9=85=8D=E7=BD=AE=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/menu/menu.js | 43 +++++++++++++++++++++++++++++++------ src/utils/routerUtil.js | 26 ++++++++++++++++------ 2 files changed, 57 insertions(+), 12 deletions(-) diff --git a/src/components/menu/menu.js b/src/components/menu/menu.js index 87075a9..f0ff305 100644 --- a/src/components/menu/menu.js +++ b/src/components/menu/menu.js @@ -38,6 +38,26 @@ import {getI18nKey} from '@/utils/routerUtil' const {Item, SubMenu} = Menu +const resolvePath = (path, params = {}) => { + let _path = path + Object.entries(params).forEach(([key, value]) => { + _path = _path.replace(new RegExp(`:${key}`, 'g'), value) + }) + return _path +} + +const toRoutesMap = (routes) => { + const map = {} + routes.forEach(route => { + map[route.fullPath] = route + if (route.children && route.children.length > 0) { + const childrenMap = toRoutesMap(route.children) + Object.assign(map, childrenMap) + } + }) + return map +} + export default { name: 'IMenu', props: { @@ -73,6 +93,9 @@ export default { computed: { menuTheme() { return this.theme == 'light' ? this.theme : 'dark' + }, + routesMap() { + return toRoutesMap(this.options) } }, created () { @@ -132,7 +155,8 @@ export default { }, renderMenuItem: function (h, menu) { let tag = 'router-link' - let config = {props: {to: menu.fullPath}, attrs: {style: 'overflow:hidden;white-space:normal;text-overflow:clip;'}} + const path = resolvePath(menu.fullPath, menu.meta.params) + let config = {props: {to: {path, query: menu.meta.query}, }, attrs: {style: 'overflow:hidden;white-space:normal;text-overflow:clip;'}} if (menu.meta && menu.meta.link) { tag = 'a' config = {attrs: {style: 'overflow:hidden;white-space:normal;text-overflow:clip;', href: menu.meta.link, target: '_blank'}} @@ -200,16 +224,23 @@ export default { }) }, updateMenu () { - const matchedRoutes = this.$route.matched.filter(item => item.path !== '') - this.selectedKeys = this.getSelectedKey(this.$route) - let openKeys = matchedRoutes.map(item => item.path) + this.selectedKeys = this.getSelectedKeys() + let openKeys = this.selectedKeys.filter(item => item !== '') openKeys = openKeys.slice(0, openKeys.length -1) if (!fastEqual(openKeys, this.sOpenKeys)) { this.collapsed || this.mode === 'horizontal' ? this.cachedOpenKeys = openKeys : this.sOpenKeys = openKeys } }, - getSelectedKey (route) { - return route.matched.map(item => item.path) + getSelectedKeys() { + let matches = this.$route.matched + const route = matches[matches.length - 1] + let chose = this.routesMap[route.path] + if (chose.meta && chose.meta.highlight) { + chose = this.routesMap[chose.meta.highlight] + const resolve = this.$router.resolve({path: chose.fullPath}) + matches = (resolve.resolved && resolve.resolved.matched) || matches + } + return matches.map(item => item.path) } }, render (h) { diff --git a/src/utils/routerUtil.js b/src/utils/routerUtil.js index f2d04a3..d16a873 100644 --- a/src/utils/routerUtil.js +++ b/src/utils/routerUtil.js @@ -44,17 +44,31 @@ function parseRoutes(routesConfig, routerMap) { router = typeof item === 'string' ? {path: item, name: item} : item } // 从 router 和 routeCfg 解析路由 + const meta = { + authority: router.authority, + icon: router.icon, + page: router.page, + link: router.link, + params: router.params, + query: router.query, + ...router.meta + } + const cfgMeta = { + authority: routeCfg.authority, + icon: routeCfg.icon, + page: routeCfg.page, + link: routeCfg.link, + params: routeCfg.params, + query: routeCfg.query, + ...routeCfg.meta + } + Object.assign(meta, cfgMeta) const route = { path: routeCfg.path || router.path || routeCfg.router, name: routeCfg.name || router.name, component: router.component, redirect: routeCfg.redirect || router.redirect, - meta: { - authority: routeCfg.authority || router.authority || routeCfg.meta?.authority || router.meta?.authority || '*', - icon: routeCfg.icon || router.icon || routeCfg.meta?.icon || router.meta?.icon, - page: routeCfg.page || router.page || routeCfg.meta?.page || router.meta?.page, - link: routeCfg.link || router.link || routeCfg.meta?.link || router.meta?.link - } + meta: {...meta, authority: meta.authority || '*'} } if (routeCfg.invisible || router.invisible) { route.meta.invisible = true