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/slot-v-slot-on-components-or-templates-only.md
1---2title: v-slot Can Only Be Used on Components or Template Tags3impact: HIGH4impactDescription: Using v-slot on HTML elements causes compilation errors5type: gotcha6tags: [vue3, slots, v-slot, compilation-error, common-mistake]7---89# v-slot Can Only Be Used on Components or Template Tags1011**Impact: HIGH** - The `v-slot` directive (and its shorthand `#`) can only be used on Vue components or `<template>` tags. Using it on native HTML elements like `<div>` or `<span>` causes a Vue compilation error.1213## Task Checklist1415- [ ] Only use `v-slot` on component elements or `<template>` tags16- [ ] When using default scoped slot shorthand, apply to the component itself17- [ ] For named slots, always use `<template #name>` syntax1819**Incorrect:**20```vue21<template>22<!-- BAD: v-slot on a native HTML element -->23<div v-slot:header>24<h1>Title</h1>25</div>2627<!-- BAD: Shorthand on HTML element -->28<span #default="{ item }">29{{ item.name }}30</span>3132<!-- BAD: v-slot inside a plain HTML element -->33<div>34<p v-slot:content>Some text</p>35</div>36</template>37```3839These cause the error: `v-slot can only be used on components or <template> tags`4041**Correct:**42```vue43<template>44<!-- GOOD: v-slot on component element (default scoped slot) -->45<MyComponent v-slot="{ item }">46{{ item.name }}47</MyComponent>4849<!-- GOOD: Named slots use template tags -->50<BaseLayout>51<template #header>52<h1>Title</h1>53</template>5455<template #default>56<p>Main content</p>57</template>5859<template #footer>60<p>Footer content</p>61</template>62</BaseLayout>6364<!-- GOOD: Shorthand on component for default slot -->65<FancyList #default="{ item }">66<div>{{ item.name }}</div>67</FancyList>68</template>69```7071## Common Scenarios7273### Wrapping Slot Content in HTML74If you need HTML wrappers around slot content, put them inside the template:7576```vue77<!-- WRONG -->78<MyComponent>79<div v-slot:header class="header-wrapper">80<h1>Title</h1>81</div>82</MyComponent>8384<!-- CORRECT -->85<MyComponent>86<template #header>87<div class="header-wrapper">88<h1>Title</h1>89</div>90</template>91</MyComponent>92```9394### Multiple v-slot on Same Element95Another error occurs when you have multiple v-slot directives - only the first is recognized:9697```vue98<!-- BAD: Multiple v-slot directives -->99<MyComponent v-slot:header v-slot:footer>100Content101</MyComponent>102103<!-- GOOD: Separate template for each slot -->104<MyComponent>105<template #header>Header</template>106<template #footer>Footer</template>107</MyComponent>108```109110## Valid v-slot Locations111112| Element Type | v-slot Allowed? | Example |113|--------------|-----------------|---------|114| Component | Yes | `<MyComponent v-slot="props">` |115| `<template>` | Yes | `<template #header>` |116| `<div>` | No | Compilation error |117| `<span>` | No | Compilation error |118| Any HTML element | No | Compilation error |119120## Reference121- [Vue.js Slots](https://vuejs.org/guide/components/slots.html)122- [DeepScan - vue-misused-v-slot](https://deepscan.io/docs/rules/vue-misused-v-slot)123