Discover how to make your Vue components talk to each other effortlessly with custom v-model!
Why Custom v-model on components in Vue? - Purpose & Use Cases
Imagine building a form with many input fields inside custom components, and you have to manually listen to events and update values everywhere.
Manually syncing data between parent and child components is tedious, error-prone, and leads to repetitive code that's hard to maintain.
Custom v-model lets you create components that behave like native inputs, automatically syncing data with simple syntax.
MyInput emits 'update:modelValue' and parent listens with @update:modelValue to update data
<MyInput v-model="formData.name" />You can build reusable, clean components that integrate seamlessly with Vue's two-way binding.
Creating a date picker component that updates the selected date in the parent form without extra event handling.
Manual data syncing between components is complex and repetitive.
Custom v-model simplifies two-way binding for your components.
This leads to cleaner, easier-to-read code and better component reuse.
Practice
v-model?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]
- Using 'value' as default prop
- Confusing event name with prop name
- Assuming 'vModel' is default prop
v-model named checked on a component?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]
- Using dot instead of colon
- Using underscore instead of colon
- Using camelCase without colon
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?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]
- Thinking prop changes inside child
- Confusing event name with prop name
- Assuming no update without v-model modifier
checked:props: ['checked'],
methods: {
updateChecked(val) {
this.$emit('update:checked', val)
}
}Parent uses:
<MyCheckbox v-model:checked="isChecked" />
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]
- Using default modelValue prop for custom v-model
- Emitting wrong event name
- Confusing event and prop names
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?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]
- Mixing default modelValue with custom names
- Emitting wrong event name
- Using wrong prop name for custom v-model
Ready to prove it?
Pass the test and this pattern moves unknown
