您好,登錄后才能下訂單哦!
小編給大家分享一下vue3.0如何實現下拉菜單的封裝,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!
先看下我們要實現的效果
很常見的展開顯示菜單項的內容,在vue3.0里面怎么開發,這里樣式我們用的是bootstrap的默認樣式
<DropDown :title="'退出'" :list="menuLists" />
<drop-down :title="'退出'"> <drop-dowm-item>新建文章</drop-down-item> <drop-dowm-item>編輯文章</drop-down-item> <drop-dowm-item>個人信息</drop-down-item> </drop-down>
兩種思路都行,相比較而言,第二種思路比較清晰,使用的時候知道具體的層次,也是elementUI組件開發的模式.
現在就第二種組件開發思路進行分析
DropDown.ts
<template> <div class="dropdown" ref="dropDownRef"> <a @click.prevent="toggleOpen" class="btn btn-secondary dropdown-toggle" href="#" rel="external nofollow" > {{ title }} </a> <div class="dropdown-menu" : v-show="isOpen"> <slot></slot> </div> </div> </template>
js部分
<script lang="ts"> import { defineComponent, ref, onMounted, onUnmounted, watch } from "vue"; import useClickOutside from "../hooks/useClickOutside"; export default defineComponent({ name: "DropDown", props: { title: { type: String, required: true, }, }, setup(context) { const isOpen = ref(false); //vue3.0獲取dom對象的引用 const dropDownRef = ref<null | HTMLElement>(null); const toggleOpen = () => { isOpen.value = !isOpen.value; }; const handleClick = (e: MouseEvent) => { console.log(e.target, "e"); if (dropDownRef.value) { console.log(dropDownRef.value); if ( //contains判斷節點是否包含節點 !dropDownRef.value.contains(e.target as HTMLElement) && isOpen.value ) { isOpen.value = false; } } }; onMounted(() => { //注冊全局的點擊事件 document.addEventListener("click", handleClick); }); onUnmounted(() => { //解綁 document.removeEventListener("click", handleClick); }); return { isOpen, toggleOpen, dropDownRef, }; }, }); </script>
DropDownItem.ts
<template> <li class="dropdowm-option" :class="{ 'is-disabled': disabled }"> <slot></slot> </li> </template> <style scoped> /* 此處是插槽需要穿透 */ .dropdowm-option.is-disabled >>> * { color: #6c757d; pointer-events: none; background-color: transparent; } </style>
<script lang="ts"> import { defineComponent } from "vue"; export default defineComponent({ props: { disabled: { type: Boolean, default: false, }, }, setup() { return {}; }, }); </script>
到這里這個組件就完成了。但是…我們可以看到點擊整個document隱藏這個事件與整個組件的關聯不大,因此我們可以抽取成一個hooks
useClickOutside.ts
import { ref, onMounted, onUnmounted,Ref } from 'vue' const useClickOutside = (elementRef:Ref<null | HTMLElement>) => { const isClickOutside = ref(false) const handler = (e: MouseEvent) => { console.log(elementRef.value); if (elementRef.value) { if (elementRef.value.contains(e.target as HTMLElement)) { isClickOutside.value = false } else { isClickOutside.value = true } } } onMounted(() => { document.addEventListener("click", handler); }); onUnmounted(() => { document.removeEventListener("click", handler); }); return isClickOutside } export default useClickOutside
然后再改寫我們的DropDown.ts組件
//刪掉之前已有的事件邏輯 <script lang="ts"> ... const isClickOutside = useClickOutside(dropDownRef); /* console.log(isClickOutside.value, "isClickOutside"); */ //引入監聽方法,數據變化時我們改變isOpen的值為false watch(isClickOutside, (newValue) => { if (isOpen.value && isClickOutside.value) { isOpen.value = false; } }); ... </script>
看完了這篇文章,相信你對“vue3.0如何實現下拉菜單的封裝”有了一定的了解,如果想了解更多相關知識,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。