Consider this Vue 3 component using the Composition API. What will be logged to the console when the button is clicked?
<template> <button @click="handleClick">Click me</button> </template> <script setup> import { ref } from 'vue' function handleClick(event) { console.log(event.target.tagName) } </script>
Remember that event.target refers to the element that triggered the event, and tagName returns the tag in uppercase.
The native event object has a target property pointing to the clicked element. The tagName property returns the tag name in uppercase, so it logs "BUTTON".
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?
Vue automatically passes the native event object as the first argument if you use $event in the template.
Option B explicitly passes $event to the handler, so the function receives the native event object. Other options either omit the event or incorrectly access it.
Examine this Vue 3 component code. Why does it cause a runtime error when clicking the button?
<template> <button @click="handleClick">Click me</button> </template> <script setup> function handleClick() { console.log(event.type) } </script>
Check how the event object is passed to event handlers in Vue 3.
The function handleClick does not receive any parameters, so event is undefined inside it. This causes a ReferenceError when trying to access event.type.
In this Vue 3 component, what will be the value of count after clicking the button two times?
<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>
The isTrusted property is true for user-generated events.
Each click triggers increment with a trusted event, so count.value increases by 1 each time. After two clicks, count is 2.
Choose the correct statement about how Vue 3 handles native event objects in event handlers.
Think about how event arguments are passed in Vue 3 templates.
In Vue 3, to access the native event object inside a handler, you must pass $event explicitly in the template. Vue does not automatically pass it.