
If you have written a custom form component in Vue, you know the ritual: declare a modelValue prop, declare an update:modelValue emit, then wire an input event that emits the new value. It works, but it is a lot of ceremony for something as common as “a component that owns a value the parent cares about.”
Vue 3.4 stabilized defineModel, a compiler macro that collapses all of that into one line. Here is what it actually does, plus the gotchas that are not obvious from the one-line happy path.
The old way, and the one-liner that replaces it
Before 3.4, a “v-model-able” input component looked like this:
<script setup>
const props = defineProps({ modelValue: String })
const emit = defineEmits(['update:modelValue'])
</script>
<template>
<input
:value="props.modelValue"
@input="emit('update:modelValue', $event.target.value)"
/>
</template>
With defineModel, the prop and the emit are declared for you, and you get a ref that reads and writes them:
<script setup>
const model = defineModel({ type: String })
</script>
<template>
<input v-model="model" />
</template>
From the parent’s perspective nothing changed — <MyInput v-model="name" /> still works. Under the hood the macro expands to a modelValue prop and an update:modelValue event, so it is fully compatible with existing parents.
Multiple models and custom names
The first argument is the model name. Without one it defaults to modelValue; with one, the parent uses the matching v-model:arg syntax:
<script setup>
const first = defineModel('first')
const last = defineModel('last')
</script>
<template>
<input v-model="first" placeholder="First" />
<input v-model="last" placeholder="Last" />
</template>
<!-- Parent -->
<UserName v-model:first="fname" v-model:last="lname" />
Each named model becomes its own first / update:first prop-event pair. This is the cleanest way to build a component that edits several fields at once.
Handling modifiers with get/set transformers
This is the part most tutorials skip. v-model modifiers (.trim, .number, .lazy) arrive on the child as a second value in the tuple returned by defineModel, and you opt into transforming reads and writes with get / set options:
<script setup>
const [model, modifiers] = defineModel({
set(value) {
// Parent wrote <MyInput v-model.trim="name" />
return modifiers.trim ? value.trim() : value
},
get(value) {
return value ?? ''
}
})
</script>
<template>
<input v-model="model" />
</template>
set runs before the value is emitted to the parent; get runs on the way into the component. That is why .trim and .number can be implemented as pure transform functions rather than manual event handlers.
Gotchas worth knowing
- It is a compile-time macro, not a runtime function. You can only use it inside
<script setup>and you cannot alias it (const dm = defineModelwill not work). The compiler needs to see the literal call. - Requires Vue 3.4+. On an older Vue the template will compile but
defineModelwill be treated as an undefined function and throw at runtime. Check yourvueversion before refactoring a shared component library. - The returned value is a ref. Inside
<script setup>it auto-unwraps in the template, but in script you needmodel.value = 'x'. Passing the whole thing intoreactive()or destructuring its.valuewill break the two-way link. - Writes are async in effect, sync in ref. Assigning to
model.valueupdates your local ref immediately but the parent’s value updates when theupdate:modelValueevent is handled during the next render. Do not read the parent’s variable on the very next line expecting it to have changed. defaultandrequiredare prop options.defineModel({ default: 'anon', required: false })passes these through to the generated prop, so validation behaves exactly like a normal prop.- Local-only mode. If you want a model that can be used without a parent
v-model(a self-contained widget), Vue 3.4 added thelocaloption:defineModel({ local: true }). Writes then stay local when no matching prop is supplied.
When to reach for it
Use defineModel for any leaf component whose main job is to edit a single value: inputs, selects, toggles, date pickers, custom steppers. It removes boilerplate and keeps the parent API identical to native v-model.
Keep the explicit prop + emit form when the child must emit several different events for one value (for example change and commit separately), or when you are on Vue < 3.4. For everything else, the one-liner is now the idiomatic choice.
TL;DR
defineModel()replacesmodelValueprop +update:modelValueemit with a single writable ref.- Name it (
defineModel('first')) for multiple models viav-model:first. - Use the
[model, modifiers]tuple withget/setto implement modifiers like.trim. - It is a
<script setup>-only compiler macro, needs Vue 3.4+, and returns a ref — mind.valuein script.