获取菜单数组的 path,最后返回的数据
[“/dashboard”, “/dashboard/analysis”, “/dashboard/analysis/realtime”, “/dashboard/analysis/offline”, “/list”]
原始的数据
const data = [
{
name: '仪表盘',
icon: 'dashboard',
path: '/dashboard', // before is 'dashboard'
children: [
{
name: '分析页',
path: '/dashboard/analysis', // before is 'analysis'
children: [
{
name: '实时数据',
path: '/dashboard/analysis/realtime', // before is 'realtime'
},
{
name: '离线数据',
path: '/dashboard/analysis/offline', // before is 'offline'
},
],
},
],
},
{
name: '列表页',
icon: 'list',
path: '/list',
},
];
打平数组菜单的 path
export function getFlatMenuKeys(menuData) {
return menuData.reduce((keys, item) => {
const {children, path} = item;
keys.push(path);
if (children && children.length) {
return keys.concat(getFlatMenuKeys(children));
}
return keys;
}, [])
}
const nestedMenu = [
{
path: "/dashboard",
children: [
{
path: "/dashboard/analysis",
children: [
{ path: "/dashboard/analysis/realtime" },
{ path: "/dashboard/analysis/offline" },
],
},
{ path: "/dashboard/monitor" },
],
},
{ path: "/user" },
{ path: "/form" },
];
// 输出
[
'/dashboard',
'/dashboard/analysis',
'/dashboard/analysis/realtime',
'/dashboard/analysis/offline',
'/dashboard/monitor',
'/user',
'/form',
]