根据子节点匹配出树形结构的路径
1 /* 递归匹配菜单路径 */ 2 recursionMenu: function(recursionField = '', menu = []) { 3 let menuTree = { 4 title: '首页', 5 url: '/pages/index', 6 children: menu 7 }; 8 let _menu = []; // 路径数组 9 let findIt = false; // 是否匹配到 10 let getPath = function(tree) { 11 _menu.push({ 12 title: tree.title, 13 path: tree.url 14 }); 15 if (tree.title === recursionField) { 16 findIt = true; 17 return; 18 } 19 if (tree.children && tree.children.length > 0) { 20 for (let i = 0; i < tree.children.length; i++) { 21 getPath(tree.children[i]); 22 if (findIt) return; 23 } 24 _menu.pop(); 25 } else if (!tree.children || tree.children.length === 0) { 26 _menu.pop(); 27 } 28 }; 29 getPath(menuTree); 30 return _menu; 31 }