获取router列表中的所有路径代码如下
/**
* 递归获取路由的所有路径
* @param router 路由列表
* @param pp ⽗级路径path parentPath
* @return []string
*/
function getPath(router,pp) {
var arr = [];
pp = pp || ''
for(let r of router){
let path = r.path
let children = r.children
if(pp){
path = `${pp}/${path}`
}
// 如果有⼦元素,不添加⽗元素的路径
if(children && children.length > 0){
arr = at(getPath(children,path))
}else{
arr.push(path)
react面试题中的router}
}
return arr;
};
测试代码如下
// 测试数据
var router = [
{
path: "/test",
children: [
{
path: "test2"
},
{
path: "test2",
children:[
{
path:'test3'
},
{
path: 'test3-1'
}
]
}
]
}
];
// 测试结果
let res = getPath(router);
//[ '/test/test2', '/test/test2/test3', '/test/test2/test3-1' ]
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论