最近使用element开发项目,在使用异步树时(即添加了lazy), 只有点击图标才会加载展开,操作体验不好,于是查询官方API尝试在节点单击时执行加载,发现没有相关接口。
经过分析源代码发现还是可以实现的。
handleExpandIconClick() {
if (this.node.isLeaf) return;
if (this.expanded) {
this.tree.$emit('node-collapse', this.node.data, this.node, this);
this.node.collapse();
} else {
this.node.expand();
this.$emit('node-expand', this.node.data, this.node, this);
}
},
从源码中可以看出在点击图标时判断节点是否已经展开,未展开就执行this.node.expand();执行数据加载并展开节点。
废话不多说,上我的代码: