el-tree的内外层数据结构不一致—用props
在使用 el-tree 组件时,内外层数据格式和属性名并不一定要一致。但是,如果内外层数据格式和属性名一致,使用起来会更加方便和简单。
如果内外层数据格式不一致,可以使用 prop 属性来指定内部节点和外部节点的对应关系,例如:
<template>
<el-tree :data="treeData" :props="treeProps"></el-tree>
</template>
<script>
export default {
data() {
return {
treeData: [
{
id: 1,
label: 'Parent',
children: [
{
childId: 2,
childLabel: 'Child'
}
]
}
],
treeProps: {
children: 'children',
label: 'label',
id: 'id'
}
}
}
}
</script>
本例中children属性里面的属性名是childId和childLabel,外面的属性名是id和label。
我们使用 treeProps 对象指定映射,其中键是 children 外部的属性名称,value 是 children 内部的属性名称。