当前位置: 首页 > news >正文

Vue3 Element-Plus el-tree 右键菜单组件

参考代码:实现Vue3+Element-Plus(tree、table)右键菜单组件

这篇文章的代码确实能用,但是存在错误,修正后的代码:

<template><div style="text-align: right"><el-icon size="12" color="#999" style="cursor: pointer"><Plus /></el-icon></div><el-tree:data="catalogTreeData":props="{ label: 'label', children: 'children' }":expand-on-click-node="false"ref="deptTreeRef"node-key="id"highlight-currentdefault-expand-all@node-contextmenu="rightClick"/><div class="rightMenu" v-show="showMenu"><ul><li v-for="(menu, index) in menus" @click="menu.click" :key="index"><el-icon><component :is="menu.icon" /></el-icon><span style="margin-left: 10px">{{ menu.name }}</span></li></ul></div>
</template>
<script lang="ts" setup>
import { FolderAdd, Message, Plus, UserFilled } from '@element-plus/icons-vue'
import { markRaw } from 'vue'
const showMenu = ref(false)const menus = ref<{icon: anyname: stringclick: () => void}[]
>([])const catalogTreeData = [{label: 'Level one 1',children: [{label: 'Level two 1-1',children: [{label: 'Level three 1-1-1'}]}]},{label: 'Level one 2',children: [{label: 'Level two 2-1',children: [{label: 'Level three 2-1-1'}]},{label: 'Level two 2-2',children: [{label: 'Level three 2-2-1'}]}]},{label: 'Level one 3',children: [{label: 'Level two 3-1',children: [{label: 'Level three 3-1-1'}]},{label: 'Level two 3-2',children: [{label: 'Level three 3-2-1'}]}]}
]// 右击方法
const rightClick = (event: MouseEvent, data: any, node: any, json: any) => {event.preventDefault()showMenu.value = false// 显示位置let menuPosition = document.querySelector('.rightMenu') as HTMLElementif (menuPosition) {menuPosition.style.left = event.clientX + 'px'menuPosition.style.top = event.clientY + 'px'}menus.value = [{icon: markRaw(FolderAdd), // 默认图标name: '新增子目录', // 默认名称click: () => {console.log('新增子目录')}},{icon: markRaw(Message),name: '编辑',click: () => {console.log('编辑')}},{icon: markRaw(UserFilled),name: '删除',click: () => {console.log('删除')}}]showMenu.value = truedocument.addEventListener('click', close)
}function close() {showMenu.value = false
}
</script>
<style scoped>
.rightMenu {position: fixed;z-index: 99999999;cursor: pointer;border: 1px solid #eee;box-shadow: 0 0.5em 1em 2px rgba(0, 0, 0, 0.1);border-radius: 6px;color: #606266;font-size: 14px;background: #fff;
}.rightMenu ul {list-style: none;margin: 0;padding: 0;border-radius: 6px;
}.rightMenu ul li {padding: 6px 10px;border-bottom: 1px solid #c8c9cc;box-sizing: border-box;display: flex;align-items: center;justify-content: flex-start;
}.rightMenu ul li:last-child {border: none;
}.rightMenu ul li:hover {transition: all 0.5s;background: #ebeef5;
}
.rightMenu ul li:hover {transition: all 0.5s;background: #ebeef5;
}.rightMenu ul li:first-child {border-radius: 6px 6px 0 0;
}
.rightMenu ul li:last-child {border-radius: 0 0 6px 6px;
}
</style>
http://www.lryc.cn/news/523150.html

相关文章:

  • 鸿蒙学习构建视图的基本语法(二)
  • python-leetcode-存在重复元素 II
  • P6周:VGG-16算法-Pytorch实现人脸识别
  • BeanFactory 是什么?它与 ApplicationContext 有什么区别?
  • 虚幻基础-1:cpu挑选(14600kf)
  • 多种vue前端框架介绍
  • jenkins-node节点配置
  • 计算机网络 (50)两类密码体制
  • 基于SpringBoot+Vue旅游管理系统的设计和实现(源码+文档+部署讲解)
  • 计算机网络-概述
  • Jenkins-基于Role的鉴权机制
  • 计算机网络介质访问控制全攻略:从信道划分到协议详解!!!
  • 5.若依 Configuration ConfigurationProperties 使用
  • 使用docker部署mysql和tomcat服务器发现的问题整理
  • 数据库开发支持服务
  • 通过学习更多样化的生成数据进行更广泛的数据分发来改进实例分割
  • NVIDIA视频编解码
  • GCC支持Objective C的故事?Objective-C?GCC只能编译C语言吗?Objective-C 1.0和2.0有什么区别?
  • 详解深度学习中的Dropout
  • SQL-杂记1
  • Python(十七)excel指定列自动翻译成英文
  • Ubuntu20.04取消root账号自动登录的方法,触觉智能RK3568开发板演示
  • 诡异的Spring @RequestBody驼峰命名字段映射失败为null问题记录
  • YOLOv10改进,YOLOv10检测头融合RFAConv卷积,添加小目标检测层(四头检测)+CA注意机制,全网首发
  • 周末总结(2024/01/18)
  • LLM - 大模型 ScallingLaws 的迁移学习与混合训练(PLM) 教程(3)
  • 【软件开发过程管理规范】需求管理,需求分析,设计开发管理,测试管理(Word)
  • 计算机网络 (49)网络安全问题概述
  • RPA编程实践:Electron实践开始
  • ORB-SLAM2源码学习:MapPoint.cc④: 新增地图点总结