TExcelTAD
Home › Vue › Vue Fallthrough Attributes

Vue Fallthrough Attributes

When you write <fancy-button class="wide" id="buy" @click="buy">, which of those does the component receive? Anything that is not declared as a prop or in emits is a fallthrough attribute. Vue automatically adds fallthrough attributes to the component's root element. Typical examples are class, style, id, title, aria-*, data-* and native listeners like @click.

Automatic inheritance

html · try it yourself
<style>
  .btn { padding: 6px 14px; border: none; border-radius: 6px; background: #42b883; color: white; }
  .danger { background: #dc2626; }
  .wide { width: 200px; }
</style>

<div id="app">
  <fancy-button>Plain</fancy-button>
  <fancy-button class="danger wide" title="Deletes everything" @click="log('delete clicked')">Delete</fancy-button>
  <fancy-button style="font-size: 20px" disabled>Disabled</fancy-button>
  <button @click="inspect">Inspect the rendered HTML</button>
</div>

<script type="module">
  import { createApp } from 'vue';

  const app = createApp({
    methods: {
      log: (m) => console.log(m),
      inspect() {
        document.querySelectorAll('#app .btn').forEach(b => console.log(b.outerHTML));
      }
    }
  });

  app.component('fancy-button', {
    template: `<button class="btn"><slot></slot></button> `
  });

  app.mount('#app');
</script>

fancy-button declares no props at all, yet the second button got the danger wide classes (merged with its own btn), the title, and the click listener. The third button became disabled.

What counts as a fallthrough attribute?

Passed to the component Falls through?
Declared in props ❌ no — becomes a prop
Listener declared in emits ❌ no — becomes a component event
class, style ✅ yes — merged with the root's own class/style
Other attributes (id, title, aria-label, data-x…) ✅ yes — added, override the root's value
Undeclared listeners (@click, @focus) ✅ yes — become native listeners on the root

Accessing $attrs

All fallthrough attributes are collected in $attrs (Options API / template) or in attrs from the setup context. This is useful for debugging and for forwarding them elsewhere.

Disabling inheritance with inheritAttrs: false

Sometimes the root element is the wrong target. A common example: a component whose root is a wrapper <label> but whose inner <input> should get placeholder, type, maxlength… Set inheritAttrs: false and bind $attrs where you want them.

html · try it yourself
<style>
  .field { display: block; margin: 8px 0; padding: 8px; background: #f0fdf4; border-radius: 6px; }
  .field input { display: block; margin-top: 4px; }
</style>

<div id="app">
  <text-field label="Email" type="email" placeholder="you@example.com" maxlength="40" class="highlight"></text-field>
  <text-field label="PIN" type="password" inputmode="numeric" maxlength="4"></text-field>
  <button @click="inspect">Log $attrs of the inputs</button>
</div>

<script type="module">
  import { createApp } from 'vue';

  const app = createApp({
    methods: {
      inspect() {
        document.querySelectorAll('#app input').forEach(i =>
          console.log(i.outerHTML));
      }
    }
  });

  app.component('text-field', {
    inheritAttrs: false,
    props: ['label'],
    template: `
      <label class="field">
        {{ label }}
        <input v-bind="$attrs">
      </label>`
  });

  app.mount('#app');
</script>

Now type, placeholder and maxlength land on the <input> instead of the <label>. Note that with inheritAttrs: false, class and style are part of $attrs too, so they also moved to the input.

Components with several root nodes

Vue 3 components may have more than one root element (a fragment). Then Vue can't know where fallthrough attributes should go, so it does not apply them automatically — you must bind $attrs explicitly on one element:

js
template: `
  <header>…</header>
  <main v-bind="$attrs">…</main>
  <footer>…</footer>`

In development builds Vue warns when attributes are passed to a multi-root component that doesn't bind $attrs.

In the Composition API

js
setup(props, { attrs }) {
  // attrs is always up to date, but NOT reactive:
  // read it in templates or event handlers, not in watch/computed.
  console.log(attrs.placeholder);
}

In <script setup> use useAttrs() and defineOptions({ inheritAttrs: false }).

Tip: Fallthrough attributes are what make wrapper components pleasant to use: <base-button aria-label="Close" @click="close"> just works, without declaring ariaLabel or click.

Note: A listener for an event that the component declares in emits doesn't fall through. If a component both emits click and its root is a <button>, declare emits: ['click'] or the handler would also be attached to the native button click and run twice.

Test yourself

A component declares no props. The parent writes <my-card id="main">. Where does id end up?

  1. Nowhere, it is ignored
  2. On the component's root element
  3. In data
  4. On every element in the template

What happens to class when both the parent and the root element set it?

  1. The parent's class replaces the root's
  2. The root's class wins
  3. They are merged
  4. Vue throws an error

How do you apply fallthrough attributes to an inner element instead of the root?

  1. Set inheritAttrs: false and use v-bind="$attrs" on the inner element
  2. Rename the attributes
  3. Use v-html
  4. Declare them all as emits