How does the v-model directive work?
The v-model directive is a two-way data binding mechanism that connects the value of an input element (input, textarea, select, component) to a variable in Vue's state.
In simpler terms:
v-modelbinds data and the UI so that changing one automatically updates the other.
How does v-model work under the hood?
When you write:
<input v-model="message">Vue turns this into two actions:
1. Binding the value (model → view)
:value="message"The UI always shows the current data.
2. An event handler (view → model)
@input="message = $event.target.value"When the user types text → the message variable updates.
It is combining these two directions that gives you two-way binding.
Example
<template>
<input v-model="name" />
<p>{{ name }}</p>
</template>
<script>
export default {
data() {
return { name: '' }
}
}
</script>The user types text → name changes.
name changes in code → the input updates.
How does v-model work on different form elements?
text input
<input v-model="text">checkbox
<input type="checkbox" v-model="checked">radio
<input type="radio" value="A" v-model="choice">
<input type="radio" value="B" v-model="choice">select
<select v-model="selected">
<option value="1">One</option>
</select>checkbox with arrays
<input type="checkbox" value="A" v-model="items">If checked → it is added to the array. If unchecked → it is removed.
v-model modifiers (Vue 3)
| Modifier | What it does |
|---|---|
.lazy | updates the model on change, not input |
.trim | removes whitespace from the edges |
.number | casts the value to a number |
Example:
<input v-model.trim.number="age">How does v-model work on components (Vue 3)
When you write:
<MyInput v-model="value" />Vue replaces this with:
- passing the
modelValueprop - listening for the
update:modelValueevent
The component must look like this:
<script>
export default {
props: ['modelValue'],
emits: ['update:modelValue']
}
</script>
<template>
<input
:value="modelValue"
@input="$emit('update:modelValue', $event.target.value)"
/>
</template>This is the internal mechanism of v-model.
Named models (Vue 3)
You can have several v-model bindings:
<DateRange
v-model:start="startDate"
v-model:end="endDate"
/>The component must support:
- a prop:
start - an event:
update:start - a prop:
end - an event:
update:end
Short Answer
Interview readyA concise answer to help you respond confidently on this topic during an interview.