What is the purpose of using a custom v-model on Vue components?
A custom v-model allows a component to define its own prop and event names for two-way binding, making it easier to integrate with parent components while keeping clear and flexible data flow.
Click to reveal answer
beginner
Which prop and event names does Vue use by default for v-model on components?
By default, Vue uses the prop named modelValue and the event named update:modelValue for v-model bindings on components.
Click to reveal answer
intermediate
How do you define a custom v-model with a different prop and event name in a Vue component?
You use the model option in the component to specify the prop and event names, for example: model: { prop: 'checked', event: 'change' }.
Click to reveal answer
intermediate
In Vue 3, how can you customize the v-model binding on a component without using the model option?
In Vue 3, you can define multiple v-model bindings by using v-model:customName syntax and then define props and emit events like customName and update:customName respectively.
Click to reveal answer
beginner
Why is it important to emit the correct event name when implementing a custom v-model?
Emitting the correct event name (like update:modelValue or update:customProp) ensures Vue detects the change and updates the bound value in the parent component, enabling two-way binding to work properly.
Click to reveal answer
What is the default prop name used by Vue's v-model on components?
AinputValue
Bvalue
Cchecked
DmodelValue
✗ Incorrect
Vue uses modelValue as the default prop name for v-model on components.
Which event should a component emit to update the default v-model binding?
Ainput
Bchange
Cupdate:modelValue
Dupdate:value
✗ Incorrect
The event update:modelValue is emitted to update the default v-model binding.
How do you bind a custom v-model named 'checked' in Vue 3?
Av-model:checked
Bv-bind:checked
Cv-model
Dv-on:checked
✗ Incorrect
Use v-model:checked to bind a custom v-model named 'checked' in Vue 3.
Which of these is NOT a correct way to customize v-model in Vue 3?
AUsing the <code>model</code> option to rename prop and event
BChanging the <code>v-model</code> directive syntax to <code>v-bind:modelValue</code>
CEmitting <code>update:customName</code> event
DUsing <code>v-model:customName</code> with matching prop and event
✗ Incorrect
Changing v-model to v-bind:modelValue does not customize v-model behavior.
Why might you want to create a custom v-model instead of using the default?
ATo use a more meaningful prop name for your component
BTo avoid two-way binding
CTo disable event emission
DTo make the component static
✗ Incorrect
Custom v-model lets you use prop names that better describe your component's data.
Explain how to create a custom v-model in Vue 3 and why it is useful.
Think about how Vue links props and events for two-way binding.
You got /4 concepts.
Describe the role of the event emitted by a component when using v-model and what happens if it is missing or incorrect.
Consider how Vue listens for changes from child components.
You got /4 concepts.
Practice
(1/5)
1. What is the default prop name used in a Vue component to work with v-model?
easy
A. inputValue
B. value
C. vModel
D. modelValue
Solution
Step 1: Understand Vue's default v-model prop
Vue uses modelValue as 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 is update:modelValue, matching the prop name.
Final Answer:
modelValue -> Option D
Quick 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
A. <MyCheckbox v-model:checked="isChecked" />
B. <MyCheckbox v-model.checked="isChecked" />
C. <MyCheckbox v-model_checked="isChecked" />
D. <MyCheckbox v-modelChecked="isChecked" />
Solution
Step 1: Recall custom v-model syntax
Vue uses v-model:customName syntax 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 A
Quick Check:
Custom v-model uses colon syntax [OK]
Hint: Use colon for custom v-model names: v-model:customName [OK]
The code uses props: ['checked'] and emits update:checked, which is correct.
Final Answer:
Prop name and event name are correct for custom v-model checked. -> Option B
Quick 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?