Preface: Sometimes part of a component's template logically belongs to that component, but from a technical point of view it would be better to move this part of the template to another place in Scenario example: a The business logic location of this modal dialog box definitely belongs to this This creates a problem: the logical location of the component is not in the same place as According to the previous 1. Teleport usageThe usage is very simple. You only need to use the to attribute to render the component to the desired position. // Render to the body tag <teleport to="body"> <div class="modal"> I'm a teleported modal! </div> </teleport> You can also use: <teleport to="#some-id" /> <teleport to=".some-class" /> <teleport to="[data-teleport]" /> Must be a valid query selector or further 2. Complete the modal dialog componentNow let's encapsulate a standard modal dialog box <template> <teleport to="body"> <transition name="dialog-fade"> <div class="dialog-wrapper" v-show="visible"> <div class="dialog"> <div class="dialog-header"> <slot name="title"> <span class="dialog-title"> {{ title }} </span> </slot> </div> <div class="dialog-body"> <slot></slot> </div> <div class="dialog-footer"> <slot name="footer"> <button @click="onClose">Close</button> </slot> </div> </div> </div> </transition> </teleport> </template> <script> import { defineComponent } from 'vue'; export default defineComponent({ name: 'tdialog' }); </script> <script setup> const props = defineProps({ title: String, visible: Boolean }); const emit = defineEmits(['close']); const onClose = () => { emit('close'); }; </script> When using, just <t-dialog title="LGD is invincible" :visible="visible" @close="onClose"> This is a piece of content, Xiaose Xianbei. </t-dialog> // ... const visible = ref(false); const onDialog = () => { visible.value = !visible.value; }; const onClose = () => { visible.value = false; }; Going a step further 3. Component rendering We have completed the standard modal dialog component above. There is another similar lightweight prompt component In the above examples, we always write the // Call out a prompt Message({ message: 'This is a Message' }); If we want to use a function to call it out, we need to prepare this function. The function of this function is to complete the rendering of the component. const Message = options => { // Prepare to render the container const container = document.createElement('div'); // Generate vnode const vnode = createVNode(MessageConstructor, options); // Renderrender(vnode, container); }; What is <template> <teleport to="#app"> <transition name="message-fade"> <div v-show="visible" ref="ins" class="message" :style="customStyle">{{ message }}</div> </transition> </teleport> </template> Online Experience View Code Summarize: The above is about the basic usage and extended usage of You may also be interested in:
|
<<: HTML Tutorial: Collection of commonly used HTML tags (6)
>>: CSS example code for implementing sliding doors
XHTML Headings Overview When we write Word docume...
Dividing lines are a common type of design on web...
This article shares the specific code for JavaScr...
background The interface domain name is not hard-...
Effect: <div class="imgs"> <!-...
How to add a loading animation every time I scrol...
Table of contents Preface Project Design rear end...
Table of contents Overview Why choose a framework...
Image tag : <img> To insert an image into a ...
** Detailed graphic instructions for installing y...
Table of contents 1. Teleport 1.1 Introduction to...
I don't know if you have noticed when making a...
Today I suddenly thought of reviewing the producti...
This article shares the specific code for JavaScr...
I used vue and bootstrap to make a relatively sim...