使用常规的方法传值
// recursion .js 转换树
const recursion = {
// 转换树结构并将需要的值存进树中
// 存值为:除了树结构必须的值外,我需要发送给后台的值 (code)
convertTree: function (tree, map) {
const result = [];
tree.forEach((item) => {
// 读取 map 的键值映射
let expand = !!item.children;
let checked = false;
let title = item[ map.title ];
let code = item[ map.code ];
let children = item[ map.children ];
// 如果有子节点,递归
if (children) {
children = this.convertTree(children, map);
}
result.push({ expand, checked, code, title, children });
})
return result;
},
}
export default recursion
// vue文件中引入
import recursion from './recursion '
// 后台返回的字段
var data = [
{"id":"0E8A7BEC-5D6C-4626-9C66-47C97A9ACC84","parentId":"00000000-0000-0000-0000-000000000000","isFinal":0,"treeLevel":1,"children":[
{"id":"0E8A7BEC-5D6C-4626-9C66-47C97A9ACC845","parentId":"00000000-0000-0000-0000-000000000001","isFinal":1,"treeLevel":1,"children":null,"name":"天凝镇人民政府1","code":"11","remark":null,"isForbidden":0},
{"id":"0E8A7BEC-5D6C-4626-9C66-47C97A9ACC846","parentId":"00000000-0000-0000-0000-000000000002","isFinal":1,"treeLevel":1,"children":null,"name":"天凝镇人民政府2","code":"12","remark":null,"isForbidden":0}
]
,"name":"天凝镇人民政府","code":"1","remark":null,"isForbidden":0}
]
const map = {
title: 'name',
children: 'finClassList',
code: 'code'
}
this.typeTree = recursion.convertTree(data, map)