v-model helps keep your data and input fields in sync automatically. When you change the input, the data updates, and when the data changes, the input updates.
v-model for two-way binding in Vue
<input v-model="dataProperty" />The v-model directive binds the input's value to a data property.
It works with many input types like text, checkbox, radio, and select.
name data property.<input v-model="name" />isChecked boolean property.<input type="checkbox" v-model="isChecked" />
selectedOption.<select v-model="selectedOption"> <option value="A">Option A</option> <option value="B">Option B</option> </select>
This Vue component uses v-model to bind the input field to the username data. When you type in the input, the paragraph below updates automatically to show the current username.
<template>
<div>
<label for="username">Username:</label>
<input id="username" v-model="username" />
<p>Your username is: {{ username }}</p>
</div>
</template>
<script setup>
import { ref } from 'vue'
const username = ref('')
</script>v-model automatically listens for input events and updates the data.
For custom components, you can use v-model with modelValue and update:modelValue events.
Remember to initialize your data property (like using ref('') for strings) so Vue can track it.
v-model links input fields and data for easy two-way binding.
It works with many input types and updates automatically.
Use it to simplify forms and user input handling in Vue.
Practice
v-model in Vue?Solution
Step 1: Understand what
v-modeldoesv-modelconnects form inputs to data so changes in one update the other automatically.Step 2: Compare with other Vue features
Fetching data, conditional rendering, and props are unrelated to two-way binding.Final Answer:
To create two-way binding between form inputs and component data -> Option DQuick Check:
v-model = two-way binding [OK]
- Confusing v-model with fetching data
- Thinking v-model controls rendering
- Mixing v-model with props definition
name using v-model?Solution
Step 1: Recall correct v-model syntax
The correct syntax isv-model="propertyName"without colon or extra suffix.Step 2: Check each option
uses correct syntax. wrongly uses colon. uses v-bind incorrectly. misplaces the property and type.Final Answer:
<input v-model="name" type="text" /> -> Option CQuick Check:
v-model="name" is correct syntax [OK]
- Adding colon before v-model
- Using v-bind instead of v-model
- Incorrect attribute order or names
data() { return { message: 'Hello' } }
<input v-model="message" />
<p>{{ message }}</p>What will be displayed in the paragraph after the user types "Hi" in the input?
Solution
Step 1: Understand v-model behavior
Typing in the input updates themessagedata property immediately.Step 2: Check template interpolation
The paragraph displays the current value ofmessage, which changes to "Hi" after typing.Final Answer:
Hi -> Option AQuick Check:
Input changes update message = Hi [OK]
- Thinking paragraph stays static
- Confusing interpolation syntax
- Assuming input does not update data
<input v-model="userName">
Assuming
userName is defined in data, why might this cause an error?Solution
Step 1: Check HTML syntax
The input tag is not self-closed or properly closed, which is invalid in Vue templates.Step 2: Understand Vue template rules
Input elements must be self-closed like<input />or have a closing tag.Final Answer:
Missing closing tag for input element -> Option BQuick Check:
Input tags must be closed [OK]
- Forgetting to close input tags
- Thinking v-model needs colon
- Assuming v-model can't be on inputs
isChecked using v-model. Which code snippet correctly implements this with two-way binding and shows "Checked" or "Not checked" text accordingly?Solution
Step 1: Identify correct checkbox binding
Usingv-model="isChecked"binds the checkbox state two-way with the data property.Step 2: Verify conditional display
The paragraph uses a ternary expression to show "Checked" or "Not checked" based onisChecked.Final Answer:
<input type="checkbox" v-model="isChecked" /> <p>{{ isChecked ? 'Checked' : 'Not checked' }}</p> -> Option AQuick Check:
Checkbox v-model + conditional text ={{ isChecked ? 'Checked' : 'Not checked' }}
[OK]
- Using :checked instead of v-model
- Wrong attribute names like v-model.checked
- Not updating data on checkbox toggle
Ready to prove it?
Pass the test and this pattern moves unknown
