Custom v-model lets you connect a parent and child component easily. It helps sync data between them without extra code.
Custom v-model on components in Vue
Introduction
Syntax
Vue
<script setup> const props = defineProps({ modelValue: String }) const emit = defineEmits(['update:modelValue']) function updateValue(newValue) { emit('update:modelValue', newValue) } </script> <template> <input :value="modelValue" @input="e => updateValue(e.target.value)" /> </template>
The prop modelValue holds the value from the parent.
The event update:modelValue sends changes back to the parent.
Examples
v-model binds text to the child component's modelValue prop and listens for update:modelValue event.Vue
<MyInput v-model="text" />v-model:custom.Vue
<MyInput v-model:custom="text" /> // In child component: const props = defineProps({ custom: String }) const emit = defineEmits(['update:custom'])
Sample Program
This example shows a parent component using CustomInput with v-model. When you type in the input, the text updates in the parent and shows below.
Vue
<script setup> import { ref } from 'vue' const text = ref('Hello') </script> <template> <CustomInput v-model="text" /> <p>You typed: {{ text }}</p> </template> <!-- CustomInput.vue --> <script setup> const props = defineProps({ modelValue: String }) const emit = defineEmits(['update:modelValue']) function onInput(event) { emit('update:modelValue', event.target.value) } </script> <template> <input :value="modelValue" @input="onInput" aria-label="Custom input" /> </template>
Important Notes
Always use modelValue as the prop and update:modelValue as the event for default v-model.
You can add aria-label for accessibility on inputs.
Customizing v-model names helps avoid conflicts in complex components.
Summary
Custom v-model connects parent and child data easily.
Use modelValue prop and update:modelValue event by default.
Custom names are possible with v-model:customName.
Practice
1. What is the default prop name used in a Vue component to work with
v-model?easy
Solution
Step 1: Understand Vue's default
Vue usesv-modelpropmodelValueas the default prop name to bind data from parent to child.Step 2: Recall the default event name
The event emitted to update the parent isupdate:modelValue, matching the prop name.Final Answer:
modelValue -> Option DQuick Check:
Default prop for v-model = modelValue [OK]
Hint: Default v-model prop is always 'modelValue' [OK]
Common Mistakes:
- Using 'value' as default prop
- Confusing event name with prop name
- Assuming 'vModel' is default prop
2. Which of the following is the correct syntax to use a custom
v-model named checked on a component?easy
Solution
Step 1: Recall custom v-model syntax
Vue usesv-model:customNamesyntax to bind custom model names.Step 2: Match syntax with options
Only<MyCheckbox v-model:checked="isChecked" />uses the correct colon syntax for custom v-model binding.Final Answer:
<MyCheckbox v-model:checked="isChecked" /> -> Option AQuick Check:
Custom v-model uses colon syntax [OK]
Hint: Use colon for custom v-model names: v-model:customName [OK]
Common Mistakes:
- Using dot instead of colon
- Using underscore instead of colon
- Using camelCase without colon
3. Given this child component code:
And parent usage:
What happens when
props: ['modelValue'],
methods: {
updateValue(val) {
this.$emit('update:modelValue', val)
}
}And parent usage:
<ChildComponent v-model="parentData" />
What happens when
updateValue('new') is called inside the child?medium
Solution
Step 1: Understand event emission in child
The child emitsupdate:modelValuewith the new value, which Vue listens to for v-model updates.Step 2: Effect on parent data
The parent bound tov-modelreceives the event and updatesparentDatato 'new'.Final Answer:
The parent's parentData updates to 'new'. -> Option CQuick Check:
Child emits update:modelValue -> parent updates [OK]
Hint: Child emits update:modelValue event to update parent data [OK]
Common Mistakes:
- Thinking prop changes inside child
- Confusing event name with prop name
- Assuming no update without v-model modifier
4. Identify the error in this child component code for custom v-model named
Parent uses:
checked:props: ['checked'],
methods: {
updateChecked(val) {
this.$emit('update:checked', val)
}
}Parent uses:
<MyCheckbox v-model:checked="isChecked" />
medium
Solution
Step 1: Check prop and event names for custom v-model
Forv-model:checked, the prop must becheckedand eventupdate:checked.Step 2: Verify code matches custom v-model pattern
The code usesprops: ['checked']and emitsupdate:checked, which is correct.Final Answer:
Prop name and event name are correct for custom v-model checked. -> Option BQuick Check:
Custom v-model uses matching prop and update:event [OK]
Hint: Custom v-model uses prop and event names matching the model name [OK]
Common Mistakes:
- Using default modelValue prop for custom v-model
- Emitting wrong event name
- Confusing event and prop names
5. You want to create a component
ToggleSwitch that uses a custom v-model named active. Which of these code snippets correctly implements the child component's props and emits to support this?hard
Solution
Step 1: Match prop and event names to custom v-model name
Forv-model:active, the prop must beactiveand eventupdate:active.Step 2: Check each option for correct prop and event
props: ['active'], methods: { toggle() { this.$emit('update:active', !this.active) } }usesprops: ['active']and emitsupdate:active, matching the custom v-model name.Final Answer:
props: ['active'], methods: { toggle() { this.$emit('update:active', !this.active) } } -> Option AQuick Check:
Custom v-model prop and event must match model name [OK]
Hint: Custom v-model prop and event must match the model name exactly [OK]
Common Mistakes:
- Mixing default modelValue with custom names
- Emitting wrong event name
- Using wrong prop name for custom v-model
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
