📖 Scenario: You are building a simple Vue app where a parent component uses a child component to edit a user's name. You want to connect the child's input with the parent's data using a custom v-model.
🎯 Goal: Create a child component with a custom v-model binding that updates the parent's userName data reactively.
📋 What You'll Learn
Create a child component named NameInput with a prop named modelValue
Emit an update:modelValue event when the input changes
In the parent component, define a reactive userName variable initialized to 'Alice'
Use the custom v-model syntax to bind userName to the NameInput component
💡 Why This Matters
🌍 Real World
Custom v-model bindings are useful when building reusable form components that need to sync data with their parents in Vue apps.
💼 Career
Understanding custom v-models is important for frontend developers working with Vue to create clean, maintainable, and reactive UI components.
Progress0 / 4 steps
1
Setup parent data
In the parent component's <script setup>, create a reactive variable called userName and set it to the string 'Alice'.
Vue
Hint
Use ref from Vue to create a reactive variable.
2
Create child component with modelValue prop
Create a child component named NameInput with a props option that declares a modelValue prop of type String. The component should render an <input> element with its value bound to modelValue.
Vue
Hint
Use defineComponent and declare props and emits. Bind value and listen for input event.
3
Use custom v-model in parent template
In the parent component's template, use the NameInput component and bind userName to it using the custom v-model syntax: <NameInput v-model:modelValue="userName" />.
Vue
Hint
Use the v-model:modelValue directive to bind userName to NameInput.
4
Display updated userName
Below the NameInput component in the template, add a <p> element that displays the text User name is: followed by the reactive userName value using interpolation.
Vue
Hint
Use mustache syntax {{ userName }} inside a paragraph tag.
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?