0
0
Vueframework~20 mins

Custom v-model on components in Vue - Practice Problems & Coding Challenges

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

Consider a Vue 3 component MyInput that uses modelValue as the prop and emits update:modelValue. If you use v-model:customValue on MyInput but the component only listens to modelValue, what will be the displayed value?

Vue
<template>
  <MyInput v-model:customValue="text" />
  <p>{{ text }}</p>
</template>

<script setup>
import { ref } from 'vue'
import MyInput from './MyInput.vue'
const text = ref('hello')
</script>

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

<script setup>
const props = defineProps({ modelValue: String })
</script>
AThe input is disabled and does not show any value.
BThe input shows empty and typing updates 'text' correctly.
CThe input shows 'hello' and typing updates 'text' correctly.
DThe input shows empty but typing does not update 'text' because 'customValue' is not handled.
Attempts:
2 left
💡 Hint

Check if the prop and event names match the v-model argument.

📝 Syntax
intermediate
2:00remaining
Which option correctly defines a custom v-model with a different prop and event in Vue 3?

You want to create a component that uses v-model:checked where the prop is checked and the event is update:checked. Which code snippet correctly defines this?

AdefineProps({ checked: Boolean }); emits: ['update:checked']
BdefineProps({ modelValue: Boolean }); emits: ['update:checked']
CdefineProps({ checked: Boolean }); emits: ['update:modelValue']
DdefineProps({ modelValue: Boolean }); emits: ['update:modelValue']
Attempts:
2 left
💡 Hint

The prop and event names must match the v-model argument.

state_output
advanced
2:00remaining
What is the value of count after clicking the button twice in this custom v-model component?

Given the parent uses v-model:count with Counter component that emits update:count on button click, what is the final value of count?

Vue
<template>
  <Counter v-model:count="count" />
  <p>{{ count }}</p>
</template>

<script setup>
import { ref } from 'vue'
import Counter from './Counter.vue'
const count = ref(0)
</script>

<!-- Counter.vue -->
<template>
  <button @click="$emit('update:count', count + 1)">Increment</button>
</template>

<script setup>
const props = defineProps({ count: Number })
</script>
A0
B1
CNaN
D2
Attempts:
2 left
💡 Hint

Check if the component uses the prop value correctly inside the emit.

🔧 Debug
advanced
2:00remaining
Why does this custom v-model component not update the parent value on input?

Analyze the code below. The parent uses v-model:inputValue but typing in the input does not update the parent. What is the cause?

Vue
<template>
  <input :value="inputValue" @input="$emit('update:modelValue', $event.target.value)" />
</template>

<script setup>
const props = defineProps({ inputValue: String })
</script>
AThe emitted event name 'update:modelValue' does not match the prop 'inputValue'.
BThe input element should use v-model instead of :value and @input.
CThe prop 'inputValue' is not reactive in the parent component.
DThe component is missing a default value for 'inputValue'.
Attempts:
2 left
💡 Hint

Check if the event name matches the prop name for the custom v-model.

🧠 Conceptual
expert
3:00remaining
How does Vue 3 internally handle multiple custom v-model bindings on a single component?

When a component uses multiple v-model bindings like v-model:title and v-model:description, how does Vue 3 manage these bindings internally?

AVue only supports one v-model per component; multiple bindings cause runtime errors.
BVue creates separate props and emits for each model, matching <code>title</code> and <code>description</code> with <code>update:title</code> and <code>update:description</code> events respectively.
CVue merges all v-model bindings into a single prop and event named <code>modelValue</code> and <code>update:modelValue</code>.
DVue uses a global store to sync all v-model bindings across components automatically.
Attempts:
2 left
💡 Hint

Think about how Vue matches prop names and event names for each v-model argument.