0
0
Vueframework~20 mins

Accessing the native event object in Vue - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Native Event 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 clicking the button?

Consider this Vue 3 component using the Composition API. What will be logged to the console when the button is clicked?

Vue
<template>
  <button @click="handleClick">Click me</button>
</template>

<script setup>
import { ref } from 'vue'

function handleClick(event) {
  console.log(event.target.tagName)
}
</script>
Anull
B"button"
C"BUTTON"
Dundefined
Attempts:
2 left
💡 Hint

Remember that event.target refers to the element that triggered the event, and tagName returns the tag in uppercase.

📝 Syntax
intermediate
2:00remaining
Which option correctly accesses the native event object in Vue 3?

In Vue 3, you want to access the native event object inside a method triggered by a click. Which code snippet correctly receives the event?

A
&lt;button @click="handleClick()"&gt;Click&lt;/button&gt;

&lt;script setup&gt;
function handleClick(event) {
  console.log(event.type)
}
&lt;/script&gt;
B
&lt;button @click="handleClick($event)"&gt;Click&lt;/button&gt;

&lt;script setup&gt;
function handleClick(event) {
  console.log(event.type)
}
&lt;/script&gt;
C
&lt;button @click="handleClick"&gt;Click&lt;/button&gt;

&lt;script setup&gt;
function handleClick() {
  console.log(event.type)
}
&lt;/script&gt;
D
&lt;button @click="handleClick"&gt;Click&lt;/button&gt;

&lt;script setup&gt;
function handleClick(event) {
  console.log(event.nativeEvent.type)
}
&lt;/script&gt;
Attempts:
2 left
💡 Hint

Vue automatically passes the native event object as the first argument if you use $event in the template.

🔧 Debug
advanced
2:00remaining
Why does this Vue 3 code cause an error when accessing the event?

Examine this Vue 3 component code. Why does it cause a runtime error when clicking the button?

Vue
<template>
  <button @click="handleClick">Click me</button>
</template>

<script setup>
function handleClick() {
  console.log(event.type)
}
</script>
A"event" is not defined inside the function scope, causing a ReferenceError.
BThe event object is null because the button has no event attached.
CThe event object is undefined because Vue 3 does not support native events.
DThe event object is accessible only in the template, not in script setup.
Attempts:
2 left
💡 Hint

Check how the event object is passed to event handlers in Vue 3.

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(event) {
  if (event.isTrusted) {
    count.value += 1
  }
}
</script>
A2
BNaN
C0
D1
Attempts:
2 left
💡 Hint

The isTrusted property is true for user-generated events.

🧠 Conceptual
expert
2:00remaining
Which statement about accessing native events in Vue 3 is correct?

Choose the correct statement about how Vue 3 handles native event objects in event handlers.

AVue 3 automatically passes the native event object as the first argument to event handlers without needing <code>$event</code>.
BVue 3 replaces the native event object with a custom event object that lacks standard properties like <code>target</code>.
CThe native event object is accessible via <code>this.$event</code> inside the handler function in Vue 3's Composition API.
DYou must explicitly pass <code>$event</code> in the template to receive the native event object in the handler function.
Attempts:
2 left
💡 Hint

Think about how event arguments are passed in Vue 3 templates.