Vue3.0 has been out for a while, and it is necessary to start studying it! Let’s first look at the effect we want to achieve It is very common to expand the content of the menu item. How to develop it in vue3.0? Here we use the default style of bootstrap Idea 1:<DropDown :title="'Exit'" :list="menuLists" /> Idea 2:<drop-down :title="'Exit'"> <drop-dowm-item>Create a new article</drop-down-item> <drop-dowm-item>Edit Article</drop-down-item> <drop-dowm-item>Personal Information</drop-down-item> </drop-down> Both ideas are fine. In comparison, the second idea is clearer. When using it, you know the specific level, which is also the mode of elementUI component development. 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" :style="{ display: 'block' }" v-show="isOpen"> <slot></slot> </div> </div> </template> js part <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 gets the reference of the DOM object 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 determines whether the node contains the node!dropDownRef.value.contains(e.target as HTMLElement) && isOpen.value ) { isOpen.value = false; } } }; onMounted(() => { //Register global click event document.addEventListener("click", handleClick); }); onUnmounted(() => { //Unbind 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> /* This is where the slot needs to penetrate*/ .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> At this point the component is complete. But... we can see that the event of clicking to hide the entire document is not very relevant to the entire component, so we can extract it into a hook 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 Then rewrite our DropDown.ts component //Delete the existing event logic<script lang="ts"> ... const isClickOutside = useClickOutside(dropDownRef); /* console.log(isClickOutside.value, "isClickOutside"); */ //Introduce the monitoring method, when the data changes, we change the value of isOpen to false watch(isClickOutside, (newValue) => { if (isOpen.value && isClickOutside.value) { isOpen.value = false; } }); ... </script> The same effect is achieved, and the code of the entire component is also simplified a lot! The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM. You may also be interested in:
|
<<: Solve the problem of IDEA configuring tomcat startup error
>>: Determine whether MySQL update will lock the table through examples
Win2008 R2 zip format mysql installation and conf...
Let’s take a look at the panoramic view effect: D...
When MySQL performs DDL operations such as alter ...
In MySQL, most indexes (such as PRIMARY KEY, UNIQ...
Preface There are often some articles on the Inte...
You can install Docker and perform simple operati...
Problem Description 1. Database of the collection...
Original address: https://blog.csdn.net/m0_465798...
By default, Flash will always be displayed at the ...
This article shares the specific code of JS to ac...
1. InnoDB storage engine must be used It has bett...
I was in a meeting when a colleague called to rep...
This article describes how to add or expand a dis...
Data Sheet /* Navicat SQLite Data Transfer Source...
Plot Review In the previous article, we analyzed ...