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/multi-root-component-class-attrs.md
1# Multi-Root Component Class Attribute Inheritance23## Rule45When a Vue 3 component has multiple root elements, class and style bindings from the parent will NOT automatically fall through. You must explicitly bind `$attrs.class` or `$attrs.style` to the target element.67## Why This Matters89- Vue 3 components can have multiple root elements (fragments)10- Unlike single-root components, multi-root components have no automatic attribute fallthrough11- Without explicit handling, classes and styles passed from parent are silently ignored12- Vue will emit a runtime warning, but styles/classes simply won't apply1314## Bad Code1516```vue17<!-- ChildComponent.vue - WRONG: classes from parent won't apply -->18<template>19<header>Header</header>20<main>Content</main>21<footer>Footer</footer>22</template>2324<!-- Parent usage -->25<ChildComponent class="my-custom-class" />26<!-- Result: my-custom-class is NOT applied to any element -->27```2829## Good Code3031```vue32<!-- ChildComponent.vue - CORRECT: explicitly bind $attrs.class -->33<template>34<header>Header</header>35<main :class="$attrs.class" :style="$attrs.style">Content</main>36<footer>Footer</footer>37</template>3839<!-- Or bind all attrs to one element -->40<template>41<header>Header</header>42<main v-bind="$attrs">Content</main>43<footer>Footer</footer>44</template>45```4647## Accessing $attrs in script setup4849```vue50<script setup>51import { useAttrs } from 'vue'52const attrs = useAttrs()53// attrs.class and attrs.style are available54</script>5556<template>57<header>Header</header>58<main :class="attrs.class">Content</main>59<footer>Footer</footer>60</template>61```6263## Disabling Automatic Inheritance6465For single-root components where you want to control attribute placement:6667```vue68<script>69export default {70inheritAttrs: false71}72</script>7374<script setup>75import { useAttrs } from 'vue'76const attrs = useAttrs()77</script>7879<template>80<div class="wrapper">81<input v-bind="attrs" />82</div>83</template>84```8586## Vue 2 to Vue 3 Migration Note8788In Vue 2, `$attrs` did NOT include `class` and `style`. In Vue 3, `$attrs` contains ALL attributes including `class` and `style`. This is a breaking change that affects how you handle attribute forwarding.8990## References9192- [Fallthrough Attributes](https://vuejs.org/guide/components/attrs.html)93- [Vue 3 Migration Guide - $attrs includes class & style](https://v3-migration.vuejs.org/breaking-changes/attrs-includes-class-style)94