Teleport lets you move part of your Vue app's content to a different place in the HTML. This helps keep your app organized and works well for things like modals or tooltips.
Teleport for rendering elsewhere in Vue
<teleport to="CSS selector or element">
<!-- content to render elsewhere -->
</teleport><teleport to="#modal-root"> <div class="modal">Hello from modal!</div> </teleport>
<teleport to="body"> <div class="tooltip">Tooltip text</div> </teleport>
This Vue component shows a button that toggles a modal. The modal content is rendered inside the element with id 'modal-root' using teleport. This keeps the modal outside the main app container but still controlled by Vue.
<template>
<div>
<button @click="show = !show">Toggle Modal</button>
<teleport to="#modal-root">
<div v-if="show" class="modal" role="dialog" aria-modal="true" tabindex="-1">
<p>This is a modal rendered elsewhere!</p>
<button @click="show = false">Close</button>
</div>
</teleport>
</div>
</template>
<script setup>
import { ref } from 'vue'
const show = ref(false)
</script>
<style scoped>
.modal {
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background: white;
border: 1px solid #ccc;
padding: 1rem;
box-shadow: 0 2px 8px rgba(0,0,0,0.3);
z-index: 1000;
}
</style>Make sure the target element (like #modal-root) exists in your HTML before using teleport.
Teleport keeps Vue reactivity and event handling working normally for the moved content.
Use ARIA roles and keyboard focus management for accessibility when rendering modals or dialogs.
Teleport moves Vue content to a different place in the HTML.
It is useful for modals, tooltips, and dropdowns that need to escape container limits.
Use the 'to' attribute to specify where to render the content.
Practice
<teleport> component?Solution
Step 1: Understand the role of <teleport>
The <teleport> component is designed to move its content to a different place in the HTML document, outside the current component's DOM tree.Step 2: Compare options with teleport's purpose
Options A, C, and D describe other Vue features like instance creation, conditional rendering, and list rendering, which are unrelated to teleport.Final Answer:
To render its children in a different part of the DOM outside the current component -> Option DQuick Check:
Teleport moves content elsewhere = B [OK]
- Confusing teleport with v-if or v-for
- Thinking teleport creates new Vue instances
- Assuming teleport only hides content
modal-root?Solution
Step 1: Recall the correct attribute name for teleport
The official attribute to specify where to send the content isto.Step 2: Check each option's attribute
Only Content usesto. Options B, C, and D use incorrect attribute names that Vue does not recognize for teleport.Final Answer:
<teleport to="#modal-root">Content</teleport> -> Option BQuick Check:
Use 'to' attribute for teleport target = A [OK]
- Using 'target' or 'destination' instead of 'to'
- Forgetting the '#' for id selector
- Using invalid attribute names
<div>
<teleport to="#outside">
<button>Click Me</button>
</teleport>
</div>
<div id="outside"></div>Where will the button appear in the rendered HTML?
Solution
Step 1: Understand teleport's behavior with 'to="#outside"'
The teleport moves its children to the element with id 'outside', so the button will be rendered inside <div id="outside">.Step 2: Confirm the button is not inside the original wrapping <div>
Because teleport moves content, the button is removed from the original wrapping <div> and placed inside the target element.Final Answer:
Inside the <div id="outside"> element -> Option CQuick Check:
Teleport moves content to target element = D [OK]
- Thinking teleport content stays inside original container
- Assuming teleport duplicates content
- Believing teleport causes rendering errors
<teleport to="#nonexistent">Hello</teleport>
What will happen when this code runs?
Solution
Step 1: Check behavior when teleport target does not exist
If the target element does not exist in the DOM, Vue does not throw an error but the content is not rendered anywhere.Step 2: Confirm fallback or error behavior
Vue does not fallback to rendering inside the component nor throw an error; it silently fails to render the teleported content.Final Answer:
The content 'Hello' will not render anywhere but no error occurs -> Option AQuick Check:
Teleport to missing target = no render, no error [OK]
- Expecting a runtime error
- Assuming content renders inside current component
- Thinking Vue creates the missing element automatically
Solution
Step 1: Identify the correct teleport usage for modals
To render modal content outside the main container, use <teleport> with the 'to' attribute pointing to a container like '#modal-root'.Step 2: Evaluate each option
<template> <teleport to="#modal-root"> <div class="modal">Modal Content</div> </teleport> </template> correctly uses <teleport to="#modal-root"> wrapping the modal content. <template> <div id="modal-root"> <div class="modal">Modal Content</div> </div> </template> places modal inside the current template without teleport. <template> <teleport> <div class="modal">Modal Content</div> </teleport> </template> misses the 'to' attribute, so teleport has no target. <template> <div class="modal">Modal Content</div> </template> renders modal inside current component without teleport.Final Answer:
Use teleport with 'to="#modal-root"' to move modal content outside main container -> Option AQuick Check:
Teleport with 'to' attribute for modals = C [OK]
- Omitting the 'to' attribute in teleport
- Placing modal content inside current component without teleport
- Assuming teleport works without specifying target
Ready to prove it?
Pass the test and this pattern moves unknown
