0Pricing
Vue Academy · Lesson

defineExpose and useAttrs

Exposing component internals with defineExpose(), useAttrs() for fallthrough attributes.

defineExpose and useAttrs is a free Vue Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Vue Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Exposing and Forwarding

Two more tools round out component communication: defineExpose lets a parent reach into a child, and useAttrs gives you the leftover attributes the parent passed. Both are common in reusable components.

Components Are Closed by Default

With <script setup>, nothing inside the component is accessible from a parent template ref. This encapsulation is good, but sometimes a parent legitimately needs to call a child method.

defineExpose to Open a Door

defineExpose explicitly publishes selected methods or values to the parent. Pass an object of what you want to share.

<script setup>
import { ref } from 'vue'
const count = ref(0)
function reset() { count.value = 0 }
defineExpose({ reset, count })
</script>

Calling Exposed Methods from a Parent

The parent grabs the child with a template ref and calls the exposed method. Only the keys passed to defineExpose are visible.

<script setup>
import { ref } from 'vue'
import Child from './Child.vue'
const childRef = ref()
function doReset() { childRef.value.reset() }
</script>

<template>
  <Child ref="childRef" />
</template>

A Focus Example

Exposing a focus method lets a parent focus an input inside a custom component without breaking encapsulation.

<script setup>
import { ref } from 'vue'
const input = ref()
function focus() { input.value.focus() }
defineExpose({ focus })
</script>

What Are Fallthrough Attributes?

When a parent puts an attribute on your component that is not a declared prop or event (like class, id, or data-*), it becomes a fallthrough attribute.

Reading Attrs with useAttrs

useAttrs() returns an object of all fallthrough attributes. Use it when you need to inspect or relocate them in script.

<script setup>
import { useAttrs } from 'vue'
const attrs = useAttrs()
console.log(attrs.class, attrs.id)
</script>

Automatic Fallthrough

By default Vue applies fallthrough attrs to the single root element of your template. If your component has one root div, a parent-supplied class lands there automatically.

Disabling Auto-Fallthrough

When you want to place attrs somewhere other than the root (for example, on an inner input in a wrapper), turn off automatic fallthrough with inheritAttrs: false in a normal script block.

<script>
export default { inheritAttrs: false }
</script>
<script setup>
import { useAttrs } from 'vue'
const attrs = useAttrs()
</script>

Forwarding Attrs with v-bind

After disabling auto-fallthrough, forward the attrs to the element you choose with v-bind="attrs". This is the standard pattern for input wrappers.

<template>
  <label>
    <span>Name</span>
    <input v-bind="attrs" />
  </label>
</template>

When to Use Each

  • defineExpose: let a parent call child methods via a template ref.
  • useAttrs + inheritAttrs: false + v-bind: control exactly where parent-supplied attributes land.

Quick Check

Test your knowledge of expose and attrs.

Recap

Key points:

  • defineExpose publishes selected methods/values to the parent ref.
  • Fallthrough attrs are non-prop, non-event attributes from the parent.
  • useAttrs() reads them; auto-fallthrough targets the root element.
  • Use inheritAttrs: false + v-bind="attrs" to redirect them.

Frequently asked questions

Is the “defineExpose and useAttrs” lesson free?

Yes — the full text of “defineExpose and useAttrs” is free to read here on the web, and the Vue Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Vue Academy course, upgrade to CoddyKit PRO.

What will I learn in “defineExpose and useAttrs”?

Exposing component internals with defineExpose(), useAttrs() for fallthrough attributes. You practise Vue Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Vue Academy?

No prior experience is required. Vue Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “defineExpose and useAttrs” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Vue Academy lesson?

Yes. Every Vue Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Introduction to script setup
  2. defineProps and defineEmits
  3. defineExpose and useAttrs
  4. Importing and Using Composables in script setup
← Back to Vue Academy