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

