0
0
Vueframework~20 mins

Shorthand syntax (: and @) in Vue - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Vue Shorthand Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this Vue template snippet?

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?

AClicked 0 times
BClicked count times
CClicked NaN times
DClicked 3 times
Attempts:
2 left
💡 Hint

Remember that @click is shorthand for v-on:click and count is a reactive reference.

📝 Syntax
intermediate
1:30remaining
Which option correctly uses shorthand for binding a prop in Vue?

You want to bind a variable title to a component's header prop using shorthand syntax. Which option is correct?

A&lt;MyComponent @header="title" /&gt;
B&lt;MyComponent :@header="title" /&gt;
C&lt;MyComponent :header="title" /&gt;
D&lt;MyComponent v-bind-header="title" /&gt;
Attempts:
2 left
💡 Hint

Recall that : is shorthand for v-bind: and @ is shorthand for v-on:.

🔧 Debug
advanced
2:30remaining
Why does this Vue event handler not work as expected?

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?

ABecause <code>count</code> is not reactive; it should be a <code>ref</code>.
BBecause <code>@click</code> shorthand is invalid here.
CBecause the function <code>increment</code> is not called correctly; it needs parentheses.
DBecause the button needs a <code>v-model</code> to update <code>count</code>.
Attempts:
2 left
💡 Hint

Think about how Vue tracks changes to variables.

state_output
advanced
2:00remaining
What is the value of 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?

AMessage: Hello
BMessage: Hi
CMessage: {{ message }}
DMessage: undefined
Attempts:
2 left
💡 Hint

Consider how the @input event updates the reactive message.

🧠 Conceptual
expert
1:30remaining
Which statement about Vue's shorthand syntax is TRUE?

Choose the correct statement about the shorthand syntax : and @ in Vue templates.

AThe <code>:</code> shorthand is equivalent to <code>v-bind:</code>, and <code>@</code> is equivalent to <code>v-on:</code>.
BThe <code>:</code> shorthand is used for event listeners, and <code>@</code> is used for binding props.
CThe <code>@</code> shorthand can only be used with native DOM events, not custom events.
DThe <code>:</code> shorthand automatically converts values to strings before binding.
Attempts:
2 left
💡 Hint

Recall the full forms of : and @ in Vue.