Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use Vue3 built-in component Teleport

Shulou Source: shulou.com Published: 2022-06-03 01:36:26 11月28日 Update

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!

Tags: Components dialogs dialogs locations modes contents functions that is logic learning prompts methods scenarios properties times standards templates this is problem faceted Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Docker Redmi MariaDB Shulou Technology