// 1、直接用:default-checked-keys='checkedKey',将Id push进去,会导致没有全部选中的第二级全部选中
list.forEach(item => {
this.checkedKey.push(item)
})
// 2、this.$refs.tree.setCheckedKeys(this.defalutArr);
解决方式
setChecked
var that = this
setTimeout(function () {
list.forEach((value)=>{
//1. 勾选节点的 key 或者 data 2. boolean 类型,节点是否选中 3. boolean 类型,是否设置子节点 ,默认为 false)如果全部选中setCheckedKeys
that.$refs.tree.(value, true, false)
})
},500);
setCheckedKeys
var that = this
let list = []
setTimeout(function () {
res.data.forEach((value)=>{
list.push(value.id)
// that.$refs.tree.setCheckedKeys([])
// that.$refs.tree.setCheckedKeys(list) 也会导致没有选中的子节点全部选中
that.$nextTick(() => {
const nodes = []
list.forEach(item => {
const node = that.$refs.tree.getNode(item);
if (node.isLeaf) { //关键,过滤掉不是叶子节点的
nodes.push(item)
}
})
that.$refs.tree.setCheckedKeys(nodes,true)
})
})