<template> <div id="app"> <teleport to="#modal"> <p>Modal content</p> </teleport> <p>Main content</p> </div> </template> <!-- Assume the HTML page has a div with id="modal" outside #app -->
The <teleport> component moves its children to the target element specified by the to attribute. Here, 'Modal content' is rendered inside the element with id 'modal', which is outside the main app container. The 'Main content' remains inside the #app div.
Option D is missing the closing </teleport> tag, causing a syntax error in the Vue template. All other options have properly closed tags.
<template>
<div>
<teleport to="#modal">
<p>{{ message }}</p>
</teleport>
<button @click="updateMessage">Click me</button>
</div>
</template>
<script setup>
import { ref } from 'vue'
const message = ref('Hello')
function updateMessage() {
message.value = 'Clicked!'
}
</script>The message is a reactive ref. The Teleport content uses {{ message }}, so it updates when message.value changes. Clicking the button changes the message to 'Clicked!', which updates the text inside the #modal element.
<template>
<div>
<teleport to="#modal">
<p>Hidden content</p>
</teleport>
</div>
</template>
<!-- The HTML page does NOT have an element with id 'modal' -->Teleport moves content to the element specified by the to selector. If that element does not exist in the DOM, the content will not render anywhere visible. The other options are incorrect because Teleport supports id selectors, does not require the target inside the component, and content is not hidden by default.
Teleport lets you render content outside the normal parent-child DOM structure. This is especially helpful for UI elements like modals, popups, or tooltips that must appear above other elements and avoid CSS clipping or stacking issues. The other options describe unrelated or incorrect behaviors.