v-model on a text input. What will be displayed below the input after the user types 'hello'?<template> <input v-model="message" type="text" aria-label="Message input" /> <p>You typed: {{ message }}</p> </template> <script setup> import { ref } from 'vue' const message = ref('') </script>
v-model binds the input value to the variable reactively.When the user types 'hello', the message variable updates reactively because of v-model. The paragraph shows the current value of message, so it displays 'You typed: hello'.
v-model to bind a text input to a variable named username?v-model is a directive that binds the input's value and listens for changes automatically.Option A correctly uses v-model="username" to bind the input's value to the username variable. Option A mixes :value and v-model which is redundant and can cause issues. Option A only binds the value but does not listen for changes. Option A uses invalid syntax.
message after input event?message after the user types 'Vue' in the input?<template> <input v-model="message" type="text" aria-label="Message input" /> </template> <script setup> import { ref } from 'vue' const message = ref('') </script>
The v-model directive binds the input's value to the message variable reactively. When the user types 'Vue', message becomes 'Vue' exactly.
text variable when typing. What is the cause?<template> <input v-model="text" type="text" /> <p>{{ text }}</p> </template> <script setup> let text = '' </script>
script setup.In Vue 3 with script setup, variables must be reactive to update the UI. Declaring text with let creates a normal variable, not reactive. Using ref('') makes it reactive and updates the UI when changed.
v-model to a computed property that only has a getter (no setter), what will happen when the user types in the input?<template> <input v-model="fullName" type="text" /> </template> <script setup> import { computed, ref } from 'vue' const firstName = ref('John') const lastName = ref('Doe') const fullName = computed(() => `${firstName.value} ${lastName.value}`) </script>
v-model tries to update the bound variable on input.A computed property without a setter is read-only. v-model tries to update the value on input, but since there is no setter, Vue throws a runtime error indicating the computed property is read-only.