PrefaceWhen writing a project today, I encountered a need for a custom right-click menu. There are submenus in the menu, so recursive components are used at this time. So I wrote this article to record the process of writing recursive components. 1. Recursive componentsA recursive component, as the name suggests, calls itself within the component itself. So we first build a component and call itself within itself. The common recursive component is the tree component that is often used in our projects. Below is the source code of a recursive component I implemented to meet the project requirements. <template> <ul class="list-container"> <li v-for="(item,index) in listData" :key="index" class="list-item" @click.prevent.stop="handleClick($event,item)" @mouseover="childrenMenuIndex=index" > <span class="list-item_span"> {{item.text}} </span> <CaretRightOutlined v-if="item.children" /> <!-- Determine whether to call itself --> <div v-if="item.children&&childrenMenuIndex===index" class="context-menu context-menu_children" > <!-- Calling the component itself --> <list-comp :list-data='item.children' @hideContextMenu='hideContextMenuEvent' /> </div> </li> </ul> </template> <script> import { defineComponent, ref } from "vue"; import {CaretRightOutlined} from '@ant-design/icons-vue'; export default defineComponent({ name:'list-comp', props:{ listData:{ type:Array, default:()=>[] } }, components:{ CaretRightOutlined }, emits: "hideContextMenu" ], setup(props,{emit}){ //Click event const handleClick=(event,{text,callBack})=>{ emit('hideContextMenu'); //callBack is the callback function you passed in. If passed in, the custom callback function will be called if (callBack) { callBack(); return; } } const hideContextMenuEvent=()=>{ emit('hideContextMenu'); } //Used to identify the currently selected menu item const childrenMenuIndex=ref(-1); const eventNames=['click','contextmenu']; onMounted(()=>{ eventNames.forEach(eventName=>window.addEventListener(eventName,hideContextMenuEvent)) }) onBeforeUnmount(()=>{ eventNames.forEach(eventName=>window.removeEventListener(eventName,hideContextMenuEvent)) }) return { handleClick, childrenMenuIndex, hideContextMenuEvent } } }) </script> Precautions
2. Right-click menu componentIn my project, I use components to implement the right-click menu. Of course, it can also be implemented through plug-ins. The right-click menu here is essentially a secondary encapsulation of the recursive component. In fact, there is no need for secondary encapsulation. You can directly use the recursive component as the right-click menu. <template> <teleport to='body' > <div class="content-menu_container" :style="styleObj"> <list-comp :list-data='menuData' @hideContextMenu='windowClickHandler' /> </div> </teleport> </template> <script> import { defineComponent } from "vue"; import ListComp from "./list-comp.vue" export default defineComponent({ name:"contextMenu", components:{ ListComp }, props:{ styleObj:{ type:Object, default:()=>{} }, menuData:{ type:Array, default:()=>[] } }, emits:['closeContextMenu'], setup(props,{emit}){ const windowClickHandler=()=>{ emit('closeContextMenu') }; return { windowClickHandler, } } }) </script> Precautions When calling the right-click menu in a project, you need to disable the right-click menu event of the window itself first. Then implement your own custom menu events. The implementation code is shown below. const showContextMenu=(event)=>{ //Disable default events and prevent bubbling event.stopPropagation(); event.preventDefault(); state.showContextMenu=true; state.styleObj={ left:event.clientX+ "px", top:event.clientY+'px' } } //Monitor the right-click menu event of the window itself onMounted(()=>{ window.addEventListener('contextmenu',showContextMenu) }) onBeforeUnmount(()=>{ window.removeEventListener('contextmenu',showContextMenu) }) SummarizeThis is the end of this article about vue3 recursive component encapsulation. For more relevant vue3 recursive component encapsulation content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Detailed explanation of data types and schema optimization in MySQL
>>: Detailed steps for using jib for docker deployment in Spring Cloud
Preface When it comes to database transactions, a...
In higher versions of Tomcat, the default mode is...
Table of contents Why use Docker? Docker installa...
Without further ado Start recording docker pullin...
Currently, almost all large websites and applicat...
Table of contents Preface 1. EndPoint 2. Connecti...
Table of contents When to use Structural branches...
Find the problem Today I am going to study the to...
MySQL 8.0.22 installation and configuration metho...
Virtual machines are very convenient testing soft...
Table of contents 1. Main functions 2. Implementa...
1. Create a scheduling task instruction crontab -...
1. Who is tomcat? 2. What can tomcat do? Tomcat i...
Preface Backup is the basis of disaster recovery....
In the previous article https://www.jb51.net/arti...