defineExpose and useAttrs
Exposing component internals with defineExpose(), useAttrs() for fallthrough attributes.
defineExpose and useAttrs is a free Vue Academy lesson on CoddyKit. This is lesson 3 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Vue Academy learning path, and your progress syncs across the web and the CoddyKit app. The Vue Academy course includes 4 lessons in total.
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:
defineExposepublishes 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.
Learn HTML with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 42
- Lessons
- 156
Frequently Asked Questions
Is the “defineExpose and useAttrs” lesson free?
Yes — any 3 lessons of the Vue Academy learning path, including “defineExpose and useAttrs”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Vue Academy course includes 4 lessons in total.
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, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 4.
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
- Introduction to script setup
- defineProps and defineEmits
- defineExpose and useAttrs
- Importing and Using Composables in script setup