// 通过树节点id获取子父节点路径的函数
export const treeFindPath = (tree, func, path = []) => {
if (!tree) return []
for (const data of tree) {
path.push(data.title)
if (func(data)) return path
if (data.children) {
const findChildren = treeFindPath(data.children, func, path)
if (findChildren.length) return findChildren
}
path.pop()
}
return []
}
使用
this.currentPath = treeFindPath(this.treeData, data => data.id === id).join('/');
网友评论