Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Vue 3 debugging reference for reactivity issues, computed errors, watcher bugs, async failures, and SSR hydration problems.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
reference/teleport-target-must-exist.md
1---2title: Teleport Target Must Exist Before Mount3impact: HIGH4impactDescription: Teleport will fail silently or throw errors if target element doesn't exist when component mounts5type: gotcha6tags: [vue3, teleport, modal, dom, lifecycle]7---89# Teleport Target Must Exist Before Mount1011**Impact: HIGH** - The teleport `to` target must already exist in the DOM when the `<Teleport>` component is mounted. If the target doesn't exist, Vue will throw an error and the teleported content won't render.1213This is a common source of bugs when using modals, tooltips, or other teleported UI elements, especially when targeting Vue-rendered elements.1415## Task Checklist1617- [ ] Ensure teleport target exists in the DOM before `<Teleport>` mounts18- [ ] Place teleport containers (e.g., `#modals`, `#tooltips`) in `index.html` outside the Vue app19- [ ] If targeting Vue-rendered elements, ensure they mount before the Teleport20- [ ] Use Vue 3.5+ `defer` prop when target is rendered later in the same component tree2122**Incorrect:**23```vue24<template>25<!-- ERROR: Target doesn't exist yet when Teleport mounts -->26<Teleport to="#modal-container">27<div class="modal">Modal content</div>28</Teleport>2930<!-- Target is defined after the Teleport -->31<div id="modal-container"></div>32</template>33```3435**Correct - Option 1: External container in index.html:**36```html37<!-- index.html -->38<body>39<div id="app"></div>40<!-- Container exists before Vue app mounts -->41<div id="modals"></div>42<div id="tooltips"></div>43</body>44```4546```vue47<template>48<!-- Safe: #modals exists before any Vue component mounts -->49<Teleport to="#modals">50<div v-if="showModal" class="modal">Modal content</div>51</Teleport>52</template>53```5455**Correct - Option 2: Teleport to body:**56```vue57<template>58<!-- Safe: body always exists -->59<Teleport to="body">60<div v-if="showModal" class="modal">Modal content</div>61</Teleport>62</template>63```6465**Correct - Option 3: Vue 3.5+ defer prop:**66```vue67<template>68<!-- Works in Vue 3.5+: defer resolves target after other parts mount -->69<Teleport defer to="#late-container">70<div class="modal">Modal content</div>71</Teleport>7273<!-- Target rendered later in template -->74<div id="late-container"></div>75</template>76```7778## Defer Prop Limitations (Vue 3.5+)7980The `defer` prop only waits for elements rendered in the **same mount/update tick**:8182```vue83<template>84<!-- ERROR: defer won't help if target mounts asynchronously -->85<Teleport defer to="#async-container">86<div>Content</div>87</Teleport>8889<!-- If this component loads asynchronously, defer won't work -->90<Suspense>91<AsyncComponent /> <!-- Contains #async-container -->92</Suspense>93</template>94```9596## Common Patterns9798### Recommended: Centralized Teleport Containers99```html100<!-- index.html -->101<body>102<div id="app"></div>103104<!-- Teleport destinations outside Vue app -->105<div id="modals" aria-live="polite"></div>106<div id="notifications" aria-live="assertive"></div>107<div id="tooltips"></div>108</body>109```110111## Reference112- [Vue.js Teleport - Using with Vue-rendered Targets](https://vuejs.org/guide/built-ins/teleport.html#using-with-vue-rendered-targets)113- [Vue.js Teleport - Deferred Teleport](https://vuejs.org/guide/built-ins/teleport.html#deferred-teleport)114