From 212bc8893d5daeb54497a5a710c8fb8183ff4895 Mon Sep 17 00:00:00 2001 From: Amumu Date: Tue, 14 Aug 2018 18:08:53 +0800 Subject: [PATCH] =?UTF-8?q?menu=20key=E5=AF=B9switch=E7=9A=84=E5=AD=90?= =?UTF-8?q?=E9=A1=B5=E9=9D=A2=E6=B2=A1=E6=9C=89=E9=85=8D=E7=BD=AEmenu?= =?UTF-8?q?=E6=97=B6=E5=8F=96=E7=88=B6=E8=8F=9C=E5=8D=95=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit menu key对switch的子页面没有配置menu时取父菜单配置 --- src/common/router.js | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/common/router.js b/src/common/router.js index eeef241b..98cc6d8b 100644 --- a/src/common/router.js +++ b/src/common/router.js @@ -70,6 +70,22 @@ function getFlatMenuData(menus) { return keys; } +function findMenuKey(menuData, path) { + const menuKey = Object.keys(menuData).find(key => pathToRegexp(path).test(key)); + if (menuKey == null) { + if (path === '/') { + return null; + } + const lastIdx = path.lastIndexOf('/'); + if (lastIdx < 0) { + return null; + } + // 如果没有,使用上一层的配置 + return findMenuKey(menuData, path.substr(0, lastIdx)); + } + return menuKey; +} + export const getRouterData = app => { const routerConfig = { '/': { @@ -185,8 +201,7 @@ export const getRouterData = app => { Object.keys(routerConfig).forEach(path => { // Regular match item name // eg. router /user/:id === /user/chen - const pathRegexp = pathToRegexp(path); - const menuKey = Object.keys(menuData).find(key => pathRegexp.test(`${key}`)); + const menuKey = findMenuKey(menuData, path); let menuItem = {}; // If menuKey is not empty if (menuKey) { -- GitLab