vue3遞歸組件封裝的全過(guò)程記錄
前言
今天在寫(xiě)項(xiàng)目時(shí),遇到一個(gè)自定義右鍵菜單的需求。在菜單中還有子菜單,所以這個(gè)時(shí)候就要用到遞歸組件了。所以寫(xiě)下這篇文章來(lái)記錄一下自己編寫(xiě)遞歸組件的過(guò)程。
1、遞歸組件
遞歸組件,顧名思義就是在組件本身內(nèi)部調(diào)用自身。所以我們先構(gòu)建一個(gè)組件,并在自身內(nèi)部調(diào)用自身。常見(jiàn)的遞歸組件就是我們項(xiàng)目中經(jīng)常會(huì)用到的樹(shù)組件了。下面就是我自己實(shí)現(xiàn)的一個(gè)能夠滿足項(xiàng)目需求的遞歸組件的源碼。
<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" /> <!-- 判斷是否需要調(diào)用自身 --> <div v-if="item.children&&childrenMenuIndex===index" class="context-menu context-menu_children" > <!-- 在組件自身內(nèi)部調(diào)用自身 --> <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}){ //點(diǎn)擊事件 const handleClick=(event,{text,callBack})=>{ emit('hideContextMenu'); //callBack是你自己傳進(jìn)來(lái)的回調(diào)函數(shù),如果傳入了,則調(diào)用自定義回調(diào)函數(shù) if(callBack){ callBack(); return; } } const hideContextMenuEvent=()=>{ emit('hideContextMenu'); } //用于標(biāo)識(shí)當(dāng)前選中的菜單項(xiàng) 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>
注意事項(xiàng)
- 在遞歸組件本身內(nèi)部,調(diào)用自身時(shí),需要將在遞歸組件上接收自己通過(guò)emit發(fā)出的自定義事件,接收后,在組件內(nèi)部再次通過(guò)emit觸發(fā)自定義事件。
- 通過(guò)監(jiān)聽(tīng)click事件,可以通過(guò)emit觸發(fā)自定義事件,在組件外部監(jiān)聽(tīng);也可以直接在通過(guò) props傳遞數(shù)據(jù)到組件內(nèi)部時(shí),就自己先構(gòu)建好回調(diào),這樣就可以不用通過(guò)emit觸發(fā)自定義事件了。
- 在點(diǎn)擊遞歸組件中的菜單項(xiàng)時(shí),需要讓遞歸組件銷毀。所有我們需要在 遞歸組件內(nèi)通過(guò)事件冒泡 監(jiān)聽(tīng)click,contextmenu等事件來(lái)讓組件銷毀,然后通過(guò)emit觸發(fā)自定義事件,讓外界接收,從而達(dá)到銷毀組件的目的。
- 在遞歸組件內(nèi)部調(diào)用click事件時(shí),需要阻止事件冒泡以及默認(rèn)事件??梢栽赾lick事件后面添加click.prevent.stop來(lái)阻止事件冒泡和默認(rèn)事件。
2、右鍵菜單組件
我項(xiàng)目中使用的是組件的形式來(lái)實(shí)現(xiàn)右鍵菜單菜單的。當(dāng)然也可以通過(guò)插件的形式來(lái)實(shí)現(xiàn)。我這里的右鍵菜單本質(zhì)上就是對(duì)遞歸組件 的二次封裝,其實(shí)不用二次封裝也可以,可以直接使用遞歸組件作為右鍵菜單。
<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>
注意事項(xiàng)
在項(xiàng)目中調(diào)用右鍵菜單時(shí),需要先禁用掉window自身的右鍵菜單事件。然后實(shí)現(xiàn)自己的自定義菜單事件。實(shí)現(xiàn)代碼如下所示。
const showContextMenu=(event)=>{ //禁用默認(rèn)事件和阻止冒泡 event.stopPropagation(); event.preventDefault(); state.showContextMenu=true; state.styleObj={ left:event.clientX+ "px", top:event.clientY+'px' } } //監(jiān)聽(tīng)window自身的右鍵菜單事件 onMounted(()=>{ window.addEventListener('contextmenu',showContextMenu) }) onBeforeUnmount(()=>{ window.removeEventListener('contextmenu',showContextMenu) })
總結(jié)
到此這篇關(guān)于vue3遞歸組件封裝的文章就介紹到這了,更多相關(guān)vue3遞歸組件封裝內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
解決vue項(xiàng)目跳轉(zhuǎn)同樣的頁(yè)面不刷新的問(wèn)題思路詳解
做公司官網(wǎng)項(xiàng)目的時(shí)候遇到的場(chǎng)景,頂部導(dǎo)航欄分類商品跳轉(zhuǎn)到分類詳情,然后在分類詳情再次點(diǎn)擊頂部導(dǎo)航欄里另外的分類商品,跳到同樣的頁(yè)面數(shù)據(jù)不刷新,下面小編給大家分享解決方式,關(guān)于vue跳轉(zhuǎn)不刷新問(wèn)題感興趣的朋友一起看看吧2023-09-09vue 實(shí)現(xiàn)剪裁圖片并上傳服務(wù)器功能
這篇文章主要介紹了vue 實(shí)現(xiàn)剪裁圖片并上傳服務(wù)器功能,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2018-03-03ElementUI實(shí)現(xiàn)el-table行列合并的操作步驟
在前端開(kāi)發(fā)中,數(shù)據(jù)展示一直是一個(gè)重要的部分,而表格則是數(shù)據(jù)展示最常見(jiàn)的形式之一,ElementUI 是餓了么前端團(tuán)隊(duì)推出的一款基于 Vue 的 UI 組件庫(kù),其中的 el-table 組件是一個(gè)功能強(qiáng)大且靈活的表格組件,今天我們要詳細(xì)探討的是 el-table 的行列合并操作2024-08-08Vue3在Setup中使用axios請(qǐng)求獲取的值方式
這篇文章主要介紹了Vue3在Setup中使用axios請(qǐng)求獲取的值方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06