In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "how to use Vue3 built-in components Teleport", the content of the article is simple and clear, easy to learn and understand, now please follow the editor's ideas slowly in depth, together to study and learn "how to use Vue3 built-in components Teleport" bar!
Catalogue
1. Teleport usage
2. Complete the modal dialog box component
3. Rendering of components
Foreword:
Vue 3.0 adds a new built-in component, teleport, mainly to address the following scenarios:
Sometimes part of a component template logically belongs to that component, and from a technical point of view, it is best to move this part of the template to a location other than Vue app in the DOM
Scene example: a Button, click and exhale the modal dialog box
The business logic location of the modal dialog box must belong to the Button, but according to the DOM structure, the actual location of the modal dialog box should be in the middle of the whole application.
So there is a problem: the logical location of the component is not the same as the DOM location
According to the previous practice of Vue2, it is common to use CSS attributes such as position: fixed; to forcibly locate the dialog box in the middle of the application. There is no way to do it. The basic usage of teleport is shown below.
1. Teleport usage
The usage is very simple, you only need to use the to property to render the component to the desired location
/ / render to I'm a teleported modal under the body tag!
You can also use:
Must be a valid query selector or HTMLElement
Further
2. Complete the modal dialog box component
Now let's encapsulate a standard modal dialog.
{{title}} close import {defineComponent} from 'vue';export default defineComponent ({name:' tdialog'}); const props = defineProps ({title: String, visible: Boolean}) Const emit = defineEmits (['close']); const onClose = () = > {emit (' close');}
When using it, you only need to
This is a paragraph, bleak Xianbei. / /... const visible = ref (false); const onDialog = () = > {visible.value =! visible.value;}; const onClose = () = > {visible.value = false;}
further more
3. Rendering of components
We have completed the standard modal dialog components above, and there is another similar lightweight prompt component Message that only needs to display a small amount of text.
In the above example, we always write the TDialog component where it is used, but for this Messgae component, we call it out using a js statement when we want to prompt, similar to the following
/ / call out a prompt Message ({message: 'this is a Message message'})
To use a function to call out, we need to prepare this function, which is used to complete the rendering of the component.
Const Message = options = > {/ / prepare rendering container const container = document.createElement ('div'); / / generate vnode const vnode = createVNode (MessageConstructor, options); / / render render (vnode, container);}
What is MessageConstructor? Is our SFC (single file component):
{{message}}
Online experience
View the code
Thank you for your reading, the above is the content of "how to use Vue3 built-in components Teleport", after the study of this article, I believe you have a deeper understanding of how to use Vue3 built-in components Teleport, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.