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/fallthrough-attrs-overwrite-vue3.md
1# Fallthrough Attributes Overwrite Explicit Attributes in Vue 323## Rule45In Vue 3, fallthrough attributes overwrite explicitly set attributes on the root element (except `class` and `style` which are merged). This is a breaking change from Vue 2. To preserve explicit attribute values, use `inheritAttrs: false` and manually bind `$attrs` before the explicit attribute.67## Why This Matters89- Silent behavior change from Vue 2 to Vue 310- Can cause unexpected attribute values in migrated codebases11- Only `class` and `style` merge intelligently; other attributes are overwritten12- Affects component composition patterns and wrapper components1314## Bad Code1516```vue17<!-- Parent.vue -->18<template>19<Child msg="Passed from Parent" />20</template>2122<!-- Child.vue - UNEXPECTED BEHAVIOR -->23<template>24<GrandChild msg="Set in Child" />25</template>2627<!--28Vue 3 Result: GrandChild receives msg="Passed from Parent"29The fallthrough attribute OVERWRITES the explicit one!3031Vue 2 Result: GrandChild receives msg="Set in Child"32The explicit attribute took precedence33-->34```3536### Another common case with data attributes3738```vue39<!-- Parent.vue -->40<template>41<Button data-testid="parent-button" />42</template>4344<!-- Button.vue - WRONG: explicit data-testid is overwritten -->45<template>46<button data-testid="submit-btn">Submit</button>47</template>4849<!-- Result: <button data-testid="parent-button">Submit</button> -->50<!-- The component's intended test ID is lost! -->51```5253## Good Code5455### Option 1: Control attribute order with inheritAttrs: false5657```vue58<!-- Child.vue - CORRECT: Control attribute precedence -->59<script setup>60defineOptions({61inheritAttrs: false62})63</script>6465<template>66<!-- v-bind="$attrs" FIRST, then explicit attribute -->67<GrandChild v-bind="$attrs" msg="Set in Child" />68</template>6970<!--71Result: GrandChild receives msg="Set in Child"72Explicit attribute overrides fallthrough because it comes last73-->74```7576### Option 2: Exclude specific attrs from fallthrough7778```vue79<script setup>80import { computed, useAttrs } from 'vue'8182defineOptions({83inheritAttrs: false84})8586const attrs = useAttrs()8788// Filter out attributes you want to control explicitly89const filteredAttrs = computed(() => {90const { msg, ...rest } = attrs91return rest92})93</script>9495<template>96<GrandChild v-bind="filteredAttrs" msg="Set in Child" />97</template>98```99100### Option 3: For wrapper components, declare as prop101102```vue103<!-- Button.vue - BEST: Declare attributes you need to control -->104<script setup>105const props = defineProps({106dataTestid: {107type: String,108default: 'submit-btn'109}110})111112defineOptions({113inheritAttrs: false114})115</script>116117<template>118<button :data-testid="dataTestid" v-bind="$attrs">119<slot />120</button>121</template>122```123124## Class and Style Are Special125126Unlike other attributes, `class` and `style` merge rather than overwrite:127128```vue129<!-- Parent.vue -->130<template>131<Button class="large" style="color: red" />132</template>133134<!-- Button.vue -->135<template>136<button class="btn" style="padding: 10px">Submit</button>137</template>138139<!--140Result: <button class="btn large" style="padding: 10px; color: red">141Both classes and styles are MERGED, not overwritten142-->143```144145## Vue 2 to Vue 3 Migration Checklist146147When migrating components that rely on attribute precedence:1481491. Identify components that set explicit attributes on root elements1502. Check if parent components pass the same attributes1513. If explicit values should take precedence:152- Add `inheritAttrs: false`153- Use `v-bind="$attrs"` before explicit attributes154155## References156157- [Fallthrough Attributes](https://vuejs.org/guide/components/attrs.html)158- [Vue 3 Migration Guide - Attribute Coercion Behavior](https://v3-migration.vuejs.org/breaking-changes/)159- [Vue Fallthrough Attributes behaviour changes from Vue 2 to Vue 3](https://lukes.tips/posts/vue-3-fallthough-attributes-changes/)160