Vue Component v-model
You already use v-model on <input>. You can make your own components work with v-model too — a custom date picker, a star rating, a toggle switch, a quantity stepper. The parent then writes <star-rating v-model="score"> and the value stays in sync in both directions.
How it works
On a component, v-model is shorthand for one prop and one event:
<!-- what you write -->
<quantity-stepper v-model="qty"></quantity-stepper>
<!-- what Vue does -->
<quantity-stepper :model-value="qty" @update:model-value="newValue => qty = newValue"></quantity-stepper>So the component has to:
- accept a prop named
modelValue, and - emit
update:modelValuewith the new value when it changes.
A quantity stepper
<style>
.stepper button { width: 28px; }
.stepper span { display: inline-block; width: 32px; text-align: center; }
</style>
<div id="app">
<p>Coffee: <quantity-stepper v-model="coffee"></quantity-stepper></p>
<p>Cake: <quantity-stepper v-model="cake" :max="3"></quantity-stepper></p>
<p>Order: {{ coffee }} coffee(s), {{ cake }} cake(s) — total ${{ coffee * 3 + cake * 4 }}</p>
<button @click="coffee = 0; cake = 0">Clear order (parent)</button>
</div>
<script type="module">
import { createApp } from 'vue';
const app = createApp({ data: () => ({ coffee: 1, cake: 0 }) });
app.component('quantity-stepper', {
props: {
modelValue: { type: Number, required: true },
max: { type: Number, default: 9 }
},
emits: ['update:modelValue'],
methods: {
change(step) {
const next = Math.min(this.max, Math.max(0, this.modelValue + step));
this.$emit('update:modelValue', next);
}
},
template: `
<span class="stepper">
<button @click="change(-1)" :disabled="modelValue === 0">−</button>
<span>{{ modelValue }}</span>
<button @click="change(1)" :disabled="modelValue === max">+</button>
</span>`
});
app.mount('#app');
</script>The data still lives in the parent. The stepper only shows modelValue and asks for a change; the parent's v-model performs the update.
Wrapping a native input
A common case is a styled input component. Use a writable computed to forward v-model to the inner <input>:
<style>
.field { display: block; margin: 8px 0; font-family: sans-serif; }
.field input { display: block; padding: 6px; border: 2px solid #42b883; border-radius: 6px; }
</style>
<div id="app">
<labeled-input label="Your name" v-model="name"></labeled-input>
<labeled-input label="Your city" v-model="city"></labeled-input>
<p>{{ name }} lives in {{ city }}.</p>
</div>
<script type="module">
import { createApp, ref, computed } from 'vue';
const app = createApp({
setup() {
return { name: ref('Huy'), city: ref('Hue') };
}
});
app.component('labeled-input', {
props: ['modelValue', 'label'],
emits: ['update:modelValue'],
setup(props, { emit }) {
const value = computed({
get: () => props.modelValue,
set: (v) => emit('update:modelValue', v)
});
return { value };
},
template: `<label class="field">{{ label }} <input v-model="value"></label>`
});
app.mount('#app');
</script>Several v-models with arguments
By giving v-model an argument, a component can expose more than one model. v-model:first-name="first" uses the prop firstName and the event update:firstName.
<div id="app">
<name-editor v-model:first-name="first" v-model:last-name="last"></name-editor>
<p>Hello, {{ first }} {{ last }}!</p>
</div>
<script type="module">
import { createApp } from 'vue';
const app = createApp({ data: () => ({ first: 'Mai', last: 'Pham' }) });
app.component('name-editor', {
props: ['firstName', 'lastName'],
emits: ['update:firstName', 'update:lastName'],
template: `
<input :value="firstName" @input="$emit('update:firstName', $event.target.value)">
<input :value="lastName" @input="$emit('update:lastName', $event.target.value)">`
});
app.mount('#app');
</script>| Parent writes | Prop | Event |
|---|---|---|
v-model="x" |
modelValue |
update:modelValue |
v-model:title="x" |
title |
update:title |
v-model.trim="x" |
modelValue + modelModifiers: { trim: true } |
update:modelValue |
defineModel in Single-File Components
In a .vue file with <script setup>, Vue 3.4+ offers the defineModel() macro, which creates the prop, the event and a writable ref in one line. This only works in SFCs (it's a compiler macro), so here it is as plain code:
<!-- QuantityStepper.vue -->
<script setup>
const qty = defineModel({ type: Number, default: 0 })
</script>
<template>
<button @click="qty--">−</button>
{{ qty }}
<button @click="qty++">+</button>
</template>Assigning to qty emits update:modelValue behind the scenes. defineModel('title') creates a named model for v-model:title.
Tip: Never mutate
modelValuedirectly inside the component — it's a prop. Always emit the new value (or assign to thedefineModelref, which does that for you).
Note: In Vue 2 the default prop and event were
valueandinput. Vue 3 renamed them tomodelValue/update:modelValue, which is why old tutorials look different.
Test yourself
Which prop and event does v-model use on a component by default in Vue 3?
valueandinputmodelValueandupdate:modelValuemodelandchangev-modelandsync
What does v-model:title="t" expect the component to emit?
update:modelValuetitlechange:titleupdate:title
Where can the defineModel() macro be used?
- In any
createAppoptions object - Only in
<script setup>of a Single-File Component - Only in the root component
- In
methods