Vue — Teleport
Basic teleport
<template>
<button @click="showModal = true">Open Modal</button>
<Teleport to="body">
<div v-if="showModal" class="modal">
<p>Modal content</p>
<button @click="showModal = false">Close</button>
</div>
</Teleport>
</template>
<script setup>
import { ref } from 'vue'
const showModal = ref(false)
</script>
Teleport to specific element
<Teleport to="#modal-container">
<div class="modal">Content</div>
</Teleport>
Conditional teleport
<Teleport to="body" :disabled="isMobile">
<div class="sidebar">Content</div>
</Teleport>
Multiple teleports
<Teleport to="body">
<div class="modal-1">First</div>
</Teleport>
<Teleport to="body">
<div class="modal-2">Second</div>
</Teleport>
Practical example
<template>
<Teleport to="body">
<Transition name="fade">
<div v-if="isOpen" class="overlay">
<div class="dialog">
<slot></slot>
<button @click="isOpen = false">Close</button>
</div>
</div>
</Transition>
</Teleport>
</template>
<script setup>
import { ref } from 'vue'
const isOpen = ref(false)
defineExpose({ open: () => isOpen.value = true })
</script>
Use cases
- Modals and dialogs
- Tooltips
- Notifications
- Drop-down menus
Mini Practice
- Create a modal with Teleport
- Add transitions
- Use conditional teleport
- Build a notification system
Up Next
Continue with Suspense - Async components.
Related Topics
Frequently Asked Questions about Teleport
What is Teleport in Vue?
Teleport is a fundamental concept in Vue. This lesson explains it step by step with clear examples, making it easy for beginners to understand.
How do I learn Teleport?
Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Teleport.
Why is Teleport important in Vue?
Teleport is essential for Vue development. Understanding this concept will help you write better code and solve real-world problems more effectively.