What is two-way binding in Vue and how does v-model work?
Learn how Vue two-way binding works and how v-model syncs state with inputs and components using modelValue, events, and modifiers like .number.
Expected Interview Answer
Two-way binding keeps a piece of state and an input in sync in both directions, and Vue's v-model provides it by combining a value binding with an event listener that updates that state.
On a native input, v-model is syntactic sugar for binding :value and listening to the input event to write the new value back. On components it expands to a modelValue prop and an update:modelValue event, so a child can be driven by and update parent state. You can create multiple named models like v-model:title and use modifiers like .lazy, .number, and .trim to adjust behavior.
- Keeps UI and state automatically in sync
- Removes boilerplate event handlers for forms
- Works on native inputs and custom components
- Supports multiple named models
- Modifiers like .number and .trim clean input
AI Mentor Explanation
Two-way binding is like the scoreboard and the scorer's book being wired together: when the scorer writes a run, the board updates, and if the board operator corrects the board, the book updates too. v-model is the wiring in between, carrying every change in both directions so the two records can never silently disagree.
Step-by-Step Explanation
Step 1
Understand the two directions
State changes update the input, and user input updates the state — both automatically.
Step 2
Use v-model on inputs
Add v-model="name" to a text input to bind :value and the input event together.
Step 3
See the sugar
v-model expands to :value plus @input on native elements, writing changes back to state.
Step 4
Support it on components
Accept a modelValue prop and emit update:modelValue to make a custom component v-model-compatible.
Step 5
Apply modifiers
Use .lazy, .number, or .trim to change when and how the bound value is updated.
What Interviewer Expects
- Definition of two-way binding
- Knowledge that v-model is syntactic sugar
- The prop/event pair behind component v-model
- Awareness of modifiers like .number and .trim
- Understanding of multiple named models
Common Mistakes
- Thinking v-model is magic rather than value plus event
- Mutating a prop directly instead of emitting update:modelValue
- Forgetting .number and getting string values from number inputs
- Not knowing v-model works on custom components
- Confusing v-model with one-way v-bind
Best Answer (HR Friendly)
“Two-way binding means the data and the form field stay in sync automatically — change one and the other updates. In Vue, v-model does this for you: it fills the input from your data and writes the user's typing back into your data, saving you from writing that wiring by hand.”
Code Example
<template>
<!-- Shorthand -->
<input v-model="name" />
<!-- Equivalent expansion -->
<input :value="name" @input="name = $event.target.value" />
<!-- On a custom component -->
<CustomInput v-model="name" />
<!-- expands to :model-value + @update:model-value -->
</template>
<script setup>
import { ref } from 'vue'
const name = ref('')
</script>Follow-up Questions
- How do you make a custom component work with v-model?
- What does the .lazy modifier change about v-model?
- How do you bind multiple v-models on one component?
- Why should you never mutate the modelValue prop directly?
- How does v-model differ from one-way v-bind?
MCQ Practice
1. On a native text input, v-model is shorthand for which combination?
v-model binds :value and listens to the input event to write the new value back to state.
2. What prop and event does v-model use on a custom component (Vue 3)?
Component v-model expands to a modelValue prop and an update:modelValue event by default.
3. Which modifier casts a number input's value to an actual number?
The .number modifier converts the input string to a number before writing it to state.
Flash Cards
What is two-way binding? — State and input stay in sync both ways: state updates the input and input updates the state.
What is v-model sugar for on inputs? — :value bound to state plus an input listener that writes changes back.
Component v-model prop/event? — modelValue prop and update:modelValue event.
What does .number do? — Casts the bound input value to a number instead of a string.