el-tree 默认值与重置问题
<el-tree
:data="data"
show-checkbox
node-key="id"
:default-checked-keys="defKeys"
:props="defaultProps"
ref="tree"
>
</el-tree>
defaultProps: {
children: "children",
label: "label",
},
defKeys: [],
//data绑定的值
//node-key="id" 绑定的id
//default-checked-keys="defKeys" //默认值
//props="defaultProps" 后端返回的格式不是el-tree需要的格式需要转换
//ref="tree" 必须绑定ref才可以操作
let res = this.$refs.tree.getCheckedKeys().concat(this.$refs.tree.getHalfCheckedKeys()); console.log(res); //res就是勾选的数据 是个数组
this.$refs.tree.setCheckedKeys([]); //清空tree里面的数据添加的时候需要清空
默认值直接取出来后端返回的数据就可以了
this.defKeys=res.data.checkedKeys