因为子节点一开始没有加载出来所以getCheckedKeys()为空,点击加载后能获取到值,内部应该有存了setCheckedKeys的值,想问下能否访问那个存了值的临时变量
<template>
<div>
<el-tree ref="tree" :props="props" :load="loadNode" lazy node-key="id" show-checkbox ></el-tree>
<button @click="getCheckedKeys()">getCheckedKeys</button>
</div>
</template>
<script>
export default {
data() {
return {
props: {
label: "name",
children: "zones",
isLeaf: "leaf"
},
nameList: ["zone", "buf", "bar"],
};
},
methods: {
loadNode(node, resolve) {
if (node.level === 0) {
return resolve([
{ name: "中国", id: 1 },
{ name: "美国", id: 2 }
]);
}
if (node.level > 1) return resolve([]);
setTimeout(() => {
const data = this.nameList.map((it, idx) => {
return {
name: it,
leaf: true,
id: Number(`${node.data.id}${idx + 1}`)
};
});
resolve(data);
}, 500);
},
getCheckedKeys(){
console.log(this.$refs.tree.getCheckedKeys())
}
},
mounted(){
this.$nextTick( ()=> {
console.log('我setCheckedKeys了')
this.$refs.tree.setCheckedKeys([13])
})
}
};
</script>