Consider this Vue 3 template using shorthand syntax:
<template>
<button @click="count++">Clicked {{ count }} times</button>
</template>
<script setup>
import { ref } from 'vue'
const count = ref(0)
</script>What will the button display after clicking it 3 times?
Remember that @click is shorthand for v-on:click and count is a reactive reference.
The @click shorthand listens for clicks and increments count. Since count is a ref, count++ updates its value. After 3 clicks, it shows 'Clicked 3 times'.
You want to bind a variable title to a component's header prop using shorthand syntax. Which option is correct?
Recall that : is shorthand for v-bind: and @ is shorthand for v-on:.
Binding a prop uses v-bind: or shorthand :. So :header="title" is correct. @ is for event listeners, not props.
Look at this Vue template snippet:
<button @click="increment">Add</button>
And the script:
<script setup>
let count = 0
function increment() {
count++
}
</script>Why does clicking the button not update the displayed count?
Think about how Vue tracks changes to variables.
Variables must be reactive (like ref) for Vue to update the UI. Here, count is a plain variable, so changes don't trigger updates.
message after this event?Given this Vue 3 component snippet:
<template>
<input :value="message" @input="updateMessage" />
<p>Message: {{ message }}</p>
</template>
<script setup>
import { ref } from 'vue'
const message = ref('Hello')
function updateMessage(event) {
message.value = event.target.value
}
</script>If the user types 'Hi' in the input, what will be displayed in the paragraph?
Consider how the @input event updates the reactive message.
The @input shorthand listens for input changes and updates message.value. So the paragraph shows the typed text 'Hi'.
Choose the correct statement about the shorthand syntax : and @ in Vue templates.
Recall the full forms of : and @ in Vue.
: is shorthand for v-bind: which binds props or attributes. @ is shorthand for v-on: which listens to events. Both work for native and custom events.