el-tree组件本身是不支持双击事件的
html
<el-tree
class="tree"
:data="nodeData"
:props="defaultProps"
@node-click="handleNodeClick"
></el-tree>
注意一下,nodeData中每一项都需要一个唯一值,用来区分两次的操作,我这里用的是id
javascript
handleNodeClick(data,node,prop) {
console.log(data,node,prop);
this.nodeCount++;
if( this.preNodeId && this.nodeCount >= 2){
this.curNodeId = data.id ;
this.nodeCount = 0;
if(this.curNodeId == this.preNodeId){//第一次点击的节点和第二次点击的节点id相同
console.log('双击,执行代码写在这里');
this.curNodeId = null;
this.preNodeId = null ;
return;
}
}
this.preNodeId = data.id;
this.nodeTimer = setTimeout(() => { //300ms内没有第二次点击就把第一次点击的清空
this.preNodeId = null;
this.nodeCount = 0;
},300)
},
参考: