1.常用回显
this.$refs.tree.setCheckedKeys(this.defalutArr);
常用回显会遇到的问题:
场景:类似于配置菜单权限,不管当前有没有选中父级菜单只要当前父级中有一个子级选中就需要给后台传递过去父级id。
问题:子菜单未全部选中,但是父级id在后台返回的数据中,那么子级会默认全部选中问题,这样明显是不对的
2.解决办法
const role = [];
res.data.authoritys.forEach(f => { //为了解决父级没有全选中(直选中某个子级)但是数据存在父级id然后回显选中问题
if (!this.$refs.tree.getNode(f).childNodes || !this.$refs.tree.getNode(f).childNodes.length) {
role.push(f)
}
});
this.$refs.tree.setCheckedKeys(role);