0
0
Vueframework~20 mins

v-model for two-way binding in Vue - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Vue v-model Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output when using v-model on a custom component?

Consider a Vue 3 component using v-model on a custom child component. What will be the value of parentValue after typing 'hello' in the input?

Vue
<template>
  <ChildComponent v-model="parentValue" />
  <p>{{ parentValue }}</p>
</template>

<script setup>
import { ref } from 'vue'
const parentValue = ref('')
</script>

<!-- ChildComponent.vue -->
<template>
  <input :value="modelValue" @input="$emit('update:modelValue', $event.target.value)" />
</template>

<script setup>
const props = defineProps({ modelValue: String })
</script>
AThe paragraph shows 'hello' as typed in the input.
BThe paragraph remains empty because v-model does not work on custom components.
CThe paragraph shows '[object Object]' instead of the typed text.
DTyping in the input causes an error because 'modelValue' is not defined.
Attempts:
2 left
💡 Hint

Remember that v-model on custom components uses modelValue prop and update:modelValue event by default.

📝 Syntax
intermediate
2:00remaining
Which option correctly uses v-model with a custom argument name?

Vue 3 allows customizing the v-model argument name. Which code snippet correctly binds v-model:checked to a child component?

A
&lt;ChildComponent v-model:checked="isChecked" /&gt;

// Child props: { modelValue: Boolean }
// Emits: 'update:modelValue'
B
&lt;ChildComponent v-model="isChecked" /&gt;

// Child props: { checked: Boolean }
// Emits: 'update:checked'
C
&lt;ChildComponent v-model:checked="isChecked" /&gt;

// Child props: { checked: Boolean }
// Emits: 'update:checked'
D
&lt;ChildComponent v-model="isChecked" /&gt;

// Child props: { checked: Boolean }
// Emits: 'update:modelValue'
Attempts:
2 left
💡 Hint

Custom v-model arguments require matching prop and event names in the child.

🔧 Debug
advanced
2:00remaining
Why does this v-model binding not update the parent value?

Given this parent and child component code, why does typing in the input not update parentText?

Vue
<!-- Parent.vue -->
<template>
  <ChildComponent v-model="parentText" />
  <p>{{ parentText }}</p>
</template>
<script setup>
import { ref } from 'vue'
const parentText = ref('')
</script>

<!-- ChildComponent.vue -->
<template>
  <input :value="text" @input="$emit('update:modelValue', $event.target.value)" />
</template>
<script setup>
const props = defineProps({ text: String })
</script>
AThe parent ref is not reactive, so it does not update.
BThe parent template is missing a key attribute on ChildComponent.
CThe input event is not emitted correctly because of missing event modifiers.
DThe child uses prop 'text' but emits 'update:modelValue', so the binding fails.
Attempts:
2 left
💡 Hint

Check if the prop name and emitted event name match the v-model default.

state_output
advanced
2:00remaining
What is the value of 'count' after clicking the button twice?

In this Vue 3 component, what will be the value of count after clicking the button two times?

Vue
<template>
  <button @click="increment">Click me</button>
  <p>{{ count }}</p>
</template>
<script setup>
import { ref } from 'vue'
const count = ref(0)
function increment() {
  count.value += 1
  count.value += 1
}
</script>
A4
B2
C1
D0
Attempts:
2 left
💡 Hint

Each click calls increment which adds 2 to count.

🧠 Conceptual
expert
2:00remaining
Which statement best describes how v-model works internally in Vue 3?

Choose the most accurate description of how v-model enables two-way binding in Vue 3 components.

A<code>v-model</code> automatically syncs all props and emits all events between parent and child without explicit code.
B<code>v-model</code> binds a prop named <code>modelValue</code> and listens for <code>update:modelValue</code> events to update the parent state reactively.
C<code>v-model</code> uses a global event bus to communicate changes between components.
D<code>v-model</code> requires the child component to mutate the parent's reactive state directly.
Attempts:
2 left
💡 Hint

Think about the prop and event naming conventions v-model uses.