In this article, we will cover:
Purpose of TeleportFirst of all, when and how to use the Teleport feature. When developing larger Vue projects, you should organize your code into reusable logic. But when dealing with certain types of components, such as modals, notifications, or tooltips, the logic of the template HTML may not be in the same file as where we want the rendered element. In fact, in most cases, it's much easier to deal with these elements than with Vue's completely separate DOM. Because things like position, z-index, and styling of nested components can get tricky because you need to deal with the scope of all their parents. This is where Teleport comes in. We can write template code in the component where the logic is located because then we can use the component's data or props. Without Teleport, we also have to worry about event propagation to pass logic from child components up the DOM tree. How Teleport worksSuppose we have some child component in which we want to trigger a notification popup. As just discussed, it would be much simpler if the notifications were rendered in a completely separate DOM tree, rather than in Vue's root #app element. First edit index.html and add a <div> before </body>. //index.html <body> <div id="app"></div> <div id='portal-target'></div> </body> Next, create the component that triggers the render notification. //VuePortals.vue <template> <div class='portals'> <button @click='showNotification'> Trigger Notification! </button> <teleport to='#portal-target'> <div class='notification'> This is rendering outside of this child component! </div> </teleport> </div> </template> <script> import { ref } from 'vue' export default { setup () { const isOpen = ref(false) var closePopup const showNotification = () => { isOpen.value = true clearTimeout(closePopup) closePopup = setTimeout(() => { isOpen.value = false }, 2000) } return { isOpen, showNotification } } } </script> <style scoped> .notification { font-family: myriad-pro, sans-serif; position: fixed; bottom: 20px; left: 20px; width: 300px; padding: 30px; background-color: #fff; } </style> In this code snippet, when the button is pressed, a notification is rendered for 2 seconds. But our main goal is to use Teleport to get notifications and render outside the VUE program. As you can see, Teleport has one required attribute: to The to attribute takes a valid DOM query string, which can be:
Since we passed the code in #portal-target, the Vue program will find the #portal-target div that we included in index.html, and it will pass all the code in the portal and render it in that div. Here are the results: Inspecting the element and looking at the DOM gives a pretty clear idea of what’s going on. We can use all the logic from the VuePortals component, but we need to tell our project which template code to render somewhere else. The above is a detailed explanation of Teleport in Vue3. For more information about Teleport in Vue3, please pay attention to other related articles on 123WORDPRESS.COM! You may also be interested in:
|
<<: Implementation of IP address configuration in Centos7.5
>>: MySQL database monitoring software lepus usage problems and solutions
1. Go to the location where you want to store the...
1. Install SVN server yum install subversion 2. C...
1. Introduction to compression and packaging Comm...
Many people have been told how to compile from th...
1|0MySQL (MariaDB) 1|11. Description MariaDB data...
1. Create a centos7.6 system and optimize the sys...
Preface: Fully encapsulating a functional module ...
This article example shares the specific code of ...
What is the function of this key attribute? Let’s...
Problem description: When inserting Chinese chara...
Table of contents Preface 1. Preparation 2. Insta...
This is an official screenshot. After MySQL 5.7 i...
Big data continues to heat up, and if you are not...
Table of contents 1. Virtual Host 1.1 Virtual Hos...
Route Jump this.$router.push('/course'); ...