//循環修改tree Key 值 mapTree(org) { const haveChildren = Array.isArray(org.childCategories) && org.childCategories.length > 0; return { //分別將我們查詢出來的值做出改變他的key title: org.category, isParent: org.isParent, id: org.id, parentId: org.parentId, // expand: org.isParent, //父級是否默認展開 // data: { ...org }, //是否生成所有 //判斷它是否存在子集,若果存在就進行再次進行遍歷操作,知道不存在子集便對其他的元素進行操作 children: haveChildren ? org.childCategories.map(i => this.mapTree(i)) : [] }; },
//調用
let data = res.data.data || []; //data 是后台返回的樹形 //console.log(data); this.treedata = data.map(org => this.mapTree(org));