fix: 优化plugin-layout,插件的配置 > 关联的路由元信息

This commit is contained in:
万纯 2021-03-05 17:35:38 +08:00
parent 1a406164d5
commit b32f087e12
2 changed files with 20 additions and 34 deletions

View File

@ -1,38 +1,17 @@
export const noop = () => {}; export const noop = () => {};
const matchName = (config, name) => { const matchName = (config, name) => {
let res;
if (Array.isArray(config)) {
for (let i = 0; i < config.length; i++) {
const item = config[i];
if (item.meta && item.meta.name === name) {
res = item.meta;
res.path = item.path;
break;
}
if (item.children && item.children.length > 0) {
res = matchName(item.children, name);
if (res) {
break;
}
}
}
}
return res;
};
const matchPath = (config, path) => {
let res = {}; let res = {};
if (Array.isArray(config)) { if (Array.isArray(config)) {
for (let i = 0; i < config.length; i++) { for (let i = 0; i < config.length; i++) {
const item = config[i]; const item = config[i];
if (item.path && item.path === path) { if (item.meta && item.meta.name === name) {
res = item.meta || {}; res = item.meta || {};
res.path = item.path; res.path = item.path;
break; break;
} }
if (item.children && item.children.length > 0) { if (item.children && item.children.length > 0) {
res = matchPath(item.children, path); res = matchName(item.children, name);
if (res) { if (res) {
break; break;
} }
@ -49,16 +28,21 @@ export const fillMenuData = (menuConfig, routeConfig, dep = 0) => {
} }
const arr = []; const arr = [];
if (Array.isArray(menuConfig) && Array.isArray(routeConfig)) { if (Array.isArray(menuConfig) && Array.isArray(routeConfig)) {
menuConfig.forEach((item) => { menuConfig.forEach((menu) => {
if (item.path !== undefined && item.path !== null) { const pageConfig = {};
Object.assign(item, matchPath(routeConfig, item.path)); if (menu.name) {
} else { Object.assign(pageConfig, matchName(routeConfig, menu.name));
Object.assign(item, matchName(routeConfig, item.name));
} }
if (item.children && item.children.length > 0) { // menu的配置优先级高当menu存在配置时忽略页面的配置
item.children = fillMenuData(item.children, routeConfig, dep); Object.keys(pageConfig).forEach((prop) => {
if (menu[prop] === undefined || menu[prop] === null || menu[prop] === '') {
menu[prop] = pageConfig[prop];
}
});
if (menu.children && menu.children.length > 0) {
menu.children = fillMenuData(menu.children, routeConfig, dep);
} }
arr.push(item); arr.push(menu);
}); });
} }
return arr; return arr;

View File

@ -12,7 +12,7 @@ export default {
publicPath: '/', publicPath: '/',
access: { access: {
roles: { roles: {
admin: ["/"] admin: ["/", "https://www.baidu.com"]
} }
}, },
request: { request: {
@ -31,10 +31,12 @@ export default {
title: "Fes.js", title: "Fes.js",
footer: 'Created by MumbelFe', footer: 'Created by MumbelFe',
multiTabs: false, multiTabs: false,
navigation: 'mixin',
menus: [{ menus: [{
name: 'index' name: 'index',
}, { }, {
name: 'onepiece' name: 'onepiece',
path: 'https://www.baidu.com'
}, { }, {
name: 'store' name: 'store'
}, { }, {