Using Teleport in Vue.js 3: Concepts and Practical Examples
This article introduces Vue.js 3’s Teleport feature, explains its core concepts such as source, target and props, and provides complete code examples for creating modals and notification panels that are rendered outside the component hierarchy, highlighting the flexibility it brings to frontend development.
With the release of Vue.js 3, a new feature called Teleport was added, allowing developers to render a component’s content in any location of the HTML document. This makes it easy to create modals, dialogs, notifications, dropdowns, and other pop‑up UI elements without worrying about layout or stacking context.
Basic concepts
Teleport : the mechanism that moves content from its source to a target.
Source : the original location of the component’s markup.
Target : the destination element where the content will be rendered (e.g., body ).
Teleport Element : the element created inside the target.
Teleport Destination : a special component that defines the target.
Teleport Props : properties that can be passed to the teleport target.
Example 1 – Modal
The following component defines a modal that is teleported to body . The modal markup is placed inside a div.modal-mask element.
<code><span><span><<span>template</span>></span></span>
<span> <span><<span>div</span>></span></span>
<span> <span><<span>teleport</span> <span>to</span>=<span>"body"</span>></span></span>
<span> <span><<span>div</span> <span>class</span>=<span>"modal-mask"</span>></span></span>
<span> <span><<span>div</span> <span>class</span>=<span>"modal-wrapper"</span>></span></span>
<span> <span><<span>div</span> <span>class</span>=<span>"modal-container"</span>></span></span>
<span> <span><<span>div</span> <span>class</span>=<span>"modal-header"></span></span>
<span> <span><<span>h3</span>></span>{{ header }}<span></<span>h3</span>></span></span>
<span> <span><<span>button</span> @<span>click</span>=<span>"$emit('close')"</span>></span>X<span></<span>button</span>></span></span>
<span> <span></<span>div</span>></span></span>
<span> <span><<span>div</span> <span>class</span>=<span>"modal-body"></span></span>
<span> <span><<span>slot</span>></span></<span>slot</span>></span></span>
<span> <span></<span>div</span>></span></span>
<span> <span><<span>div</span> <span>class</span>=<span>"modal-footer"></span></span>
<span> <span><<span>button</span> @<span>click</span>=<span>"$emit('close')"</span>></span>Close<span></<span>button</span>></span></span>
<span> <span></<span>div</span>></span></span>
<span> <span></<span>div</span>></span></span>
<span> <span></<span>div</span>></span></span>
<span> <span></<span>div</span>></span></span>
<span> <span></<span>teleport</span>></span></span>
<span> <span></<span>div</span>></span></span>
<span></<span>template</span>></span></span></code>The parent component can open the modal by toggling modalOpen and passing a header prop.
<code><span><span><<span>template</span>></span></span>
<span> <span><<span>div</span>></span></span>
<span> <span><<span>button</span> @<span>click</span>=<span>"modalOpen = true"</span>></span>Open Modal<span></<span>button</span>></span></span>
<span> <span><<span>modal</span> <span>:header</span>=<span>"'My Modal'"</span> <span>v-model:show</span>=<span>"modalOpen"</span>></span></span>
<span> <span><<span>p</span>></span>Modal content<span></<span>p</span>></span></span>
<span> <span></<span>modal</span>></span></span>
<span> <span></<span>div</span>></span></span>
<span></<span>template</span>></span></span>
<span><<span>script</span>></span>
<span>import Modal from './Modal.vue'</span>
<span>export default {</span>
<span> components: { Modal },</span>
<span> data() { return { modalOpen: false } }</span>
<span>}</span>
<span></<span>script</span>></span></code>Example 2 – Notifications
This example shows how to teleport a list of notification cards to body , rendering each item from a notifications array.
<code><span><span><<span>template</span>></span></span>
<span> <span><<span>div</span>></span></span>
<span> <span><<span>teleport</span> <span>to</span>=<span>"body"</span> <span>v-if</span>=<span>"show"</span>></span></span>
<span> <span><<span>div</span> v-for="item in notifications" :key="item.id" class="notification" :class="item.type"></span></span>
<span> <span><<span>span</span> class="notification-title"></span>{{ item.title }}<span></<span>span</span>></span></span>
<span> <span><<span>p</span>></span>{{ item.content }}<span></<span>p</span>></span></span>
<span> <span></<span>div</span>></span></span>
<span> <span></<span>teleport</span>></span></span>
<span> <span></<span>div</span>></span></span>
<span></<span>template</span>></span></span>
<span><<span>script</span>></span>
<span>export default {</span>
<span> props: {</span>
<span> show: { type: Boolean, default: false },</span>
<span> notifications: { type: Array, default: () => [] }</span>
<span> }</span>
<span>}</span>
<span></<span>script</span>></span></code>The parent component toggles showNotifications and supplies an array of notification objects.
<code><span><span><<span>template</span>></span></span>
<span> <span><<span>div</span>></span></span>
<span> <span><<span>button</span> @<span>click</span>=<span>"showNotifications = !showNotifications"</span>></span>{{ showNotifications ? 'Close Notifications' : 'Open Notifications' }}<span></<span>button</span>></span></span>
<span> <span><<span>notifications</span> :show="showNotifications" :notifications="notifications" /></span></span>
<span> <span></<span>div</span>></span></span>
<span></<span>template</span>></span></span>
<span><<span>script</span>></span>
<span>import Notifications from './Notifications.vue'</span>
<span>export default {</span>
<span> components: { Notifications },</span>
<span> data() { return {</span>
<span> showNotifications: false,</span>
<span> notifications: [</span>
<span> { id: 1, type: 'success', title: 'Success', content: 'Your task has been completed successfully.' },</span>
<span> { id: 2, type: 'error', title: 'Error', content: 'An error occurred while performing your task.' }</span>
<span> ]</span>
<span> } }</span>
<span>}</span>
<span></<span>script</span>></span></code>Conclusion
Using Teleport in Vue.js 3 simplifies the management of pop‑up UI components, improves layout control, and enhances maintainability. The examples demonstrate how to create modals and notification panels that are rendered outside the normal component hierarchy, showcasing the flexibility and extensibility introduced by this new feature.
php中文网 Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.