v-model creates two-way binding on form inputs—a Vue hallmark. It is syntactic sugar for a value prop plus an input/update event listener.
Text inputs
<input v-model="email" />
<!-- equivalent idea -->
<input :value="email" @input="email = $event.target.value" />
Other controls
- Checkbox:
v-model="agreed"binds booleanchecked - Select:
v-model="role"on<select> - Custom components:
modelValueprop +update:modelValueemit in Vue 3
Compared to React
React uses controlled inputs with explicit value and onChange. Vue’s v-model reduces boilerplate while keeping the same “single source of truth in state” idea.
Important interview questions and answers
- Q: Is v-model magic global state?
A: No—it binds a local ref/reactive field to an input; still component-scoped unless you pass the ref down. - Q: v-model on custom components?
A: ImplementmodelValue+update:modelValueor named models likev-model:title.
Self-check
- What two things does
v-modelcombine? - How do you bind a checkbox boolean?
Challenge
Live greeting
- Type in the input and confirm the paragraph updates.
- Log the name on each change with
watchandprintOutput.
Done when: preview and terminal stay in sync with the input value.
Challenge
Trim on blur
- Add
v-model.trimto a text field. - Type spaces and submit—terminal JSON should omit leading/trailing space.
Done when: trimmed value matches what you would send to an API.
Interview prep
- What does v-model do on inputs?
Two-way binding sugar: keeps the input in sync with a ref—text inputs bind value + input; checkboxes bind checked state.