Attribute Inheritance — Vue

Attributes on a component's tag that aren't declared as props or in emits, such as class, id, style and @click, are passed on to that component's root…

The BaseButton component has no prop called class. Yet when the parent writes <BaseButton class="main" />, the button still gets the class main. Who put it there?

Vue did. Attributes you write on a component's tag that aren't props, such as class, id, style, type or @click, are passed on automatically to that component's root element, the outermost element in its template. Vue calls them fallthrough attributes (attributes that are passed on). Picture the cashier at Mrs. Sri's stall sticking a "SPICY" sticker on a takeaway order. The sticker goes on the outermost wrapping paper, not on the rice. If the order is two separate parcels, the cashier can't tell which one to stick it on, so the sticker isn't stuck on at all. In Vue, the sticker is an attribute from the parent, the outermost wrapping paper is the root element, and two separate parcels are a component with two root elements.

Attributes fall through to the root element

Here is the component BaseButton with one root element, a <button>:

<!-- BaseButton.vue -->
<template>
  <button class="button" type="button">Order</button>
</template>

The parent places it with several attributes:

<!-- App.vue -->
<BaseButton class="main" id="order" type="submit" style="color: white" @click="order" />

The HTML that comes out:

<button class="button main" type="submit" id="order" style="color: white;">Order</button>

$attrs: everything passed on

Every attribute that's passed on is collected in the $attrs object. In the child's template, {{ $attrs }} for the <BaseButton> tag above holds class, id, type, style and onClick (@click is kept under the name onClick). In <script setup>, you get the same object with useAttrs(), imported from "vue".

Turning inheritance off: inheritAttrs: false

Sometimes the root element is the wrong place. Say BaseButton wraps its button in a <div>. The parent's class or id should land on the <button>, not the <div>:

<!-- BaseButton.vue -->
<script setup>
defineOptions({ inheritAttrs: false })
</script>

<template>
  <div class="wrapper">
    <button v-bind="$attrs">Order</button>
  </div>
</template>

The result for <BaseButton class="main" id="order" @click="order" />:

<div class="wrapper"><button class="main" id="order">Order</button></div>

Several roots: nobody gets them

Now the BaseButton template has two root elements:

<!-- BaseButton.vue, two roots -->
<template>
  <button>Order</button>
  <small>Sold out by 9</small>
</template>

<BaseButton class="main" id="order" /> gives <button>Order</button><small>Sold out by 9</small> with no class and no id. Vue can't tell which root to put them on, so it puts them on none. In development mode, the console writes [Vue warn]: Extraneous non-props attributes (class, id) were passed to component but could not be automatically inherited because component renders fragment or text or teleport root nodes. The fix: write v-bind="$attrs" on the root that should get those attributes. The warning then disappears.

.vue files and the exercise editor

In the exercise editor, a child is written as an object and placed with <component :is>, as in the Props lesson. The attributes you write on <component> are passed on the same way:

const count = ref(0)

const BaseButton = {
  template: `<button class="button">Order</button>`,
}

---template---
<div>
  <component :is="BaseButton" class="main" id="order" @click="count++" />
  <p>Ordered: {{ count }}</p>
</div>
In a .vue file In the exercise editor
defineOptions({ inheritAttrs: false }) the inheritAttrs: false option in the BaseButton object
useAttrs() in the script $attrs in the child's template
<BaseButton class="main" /> <component :is="BaseButton" class="main" />

The exercise uses this form. Its starter code has BaseButton with the template <button class="btn">Click Me</button>, no id, and you place it with <component :is="BaseButton" id="btn" class="primary" />. That id falls through to the <button>, and primary is merged with btn. The checker looks for button#btn.btn.primary (a <button> with the id btn and the classes btn and primary), and refuses an id written straight on the <button>.

⚠️ Wrong guesses about where attributes go

TL;DR: Attributes on a component's tag that aren't declared as props or in emits, such as class, id, style and @click, are passed on to that component's root element, the outermost element in its template. class and style are merged, other attributes from the parent replace the child's, and event listeners such as @click are attached too. They're all collected in the $attrs object (useAttrs() in the script). inheritAttrs: false turns the automatic passing off, then v-bind="$attrs" puts them on the element you pick. A component with several root elements passes nothing on until you write v-bind="$attrs" yourself.

⚡ Attributes fall through to the root element

Write:

const BaseButton = {
  template: `<button class="button">Order</button>`,
}

---template---
<component :is="BaseButton" class="main" id="order" />
Guess first: BaseButton has no class prop. Which classes does its button get in the end?

button main. The parent's class is merged with the child's own, and id="order" is passed on to the <button> too.

Meaning: Attributes on a component's tag that aren't props, such as class, id, style or @click, are passed on to that component's root element (the outermost element in its template). class and style are merged, and other attributes from the parent replace the child's.

⚡ Pick the place yourself

Write:

<!-- BaseButton.vue -->
<script setup>
defineOptions({ inheritAttrs: false })
</script>

<template>
  <div class="wrapper">
    <button v-bind="$attrs">Order</button>
  </div>
</template>

Meaning: defineOptions writes component options in <script setup>. The option inheritAttrs: false turns off the automatic passing to the root element. $attrs holds every attribute passed on from the parent, and v-bind="$attrs" puts all of them, @click included, on the element you choose.

⚡ Several roots, nobody gets them

Guess first: the BaseButton template has two roots, a <button> and a <small>. Where do the parent's class and id go?

Nowhere. Vue can't tell which root should get them, so none does.

Meaning: If a component has more than one root element, Vue passes the attributes on to none of them. Write v-bind="$attrs" on the root that should receive them.

What you will learn