📖 Scenario: You are building a simple Vue 3 app where a button click shows the exact mouse coordinates of the click event.
🎯 Goal: Create a Vue 3 component that captures the native click event on a button and displays the clientX and clientY coordinates of the mouse click.
📋 What You'll Learn
Use Vue 3 Composition API with <script setup>
Create a reactive variable to store the click coordinates
Add a button with a click event listener that accesses the native event object
Display the clientX and clientY values on the page
💡 Why This Matters
🌍 Real World
Capturing native event details like mouse position is common in interactive web apps such as drawing tools, games, or custom UI controls.
💼 Career
Understanding how to access native event objects and use reactive state is essential for frontend developers working with Vue or similar frameworks.
Progress0 / 4 steps
1
Set up reactive variables for coordinates
In the <script setup> block, import ref from 'vue' and create two reactive variables called clickX and clickY initialized to 0.
Vue
Hint
Use import { ref } from 'vue' and then const clickX = ref(0) and const clickY = ref(0).
2
Add click event handler function
Below the reactive variables, create a function called handleClick that takes a parameter event. Inside the function, set clickX.value to event.clientX and clickY.value to event.clientY.
Vue
Hint
Define function handleClick(event) and update clickX.value and clickY.value using event.clientX and event.clientY.
3
Bind the click event to the button
In the <template>, add a @click event listener to the <button> that calls the handleClick function and passes the native event object.
Vue
Hint
Add @click="handleClick" to the <button> tag.
4
Add accessibility and finalize display
Add an aria-label attribute to the <button> with the value Click to get mouse coordinates. Also, ensure the coordinates are displayed inside a <p> tag with a clear label.
Vue
Hint
Add aria-label="Click to get mouse coordinates" to the <button> tag for accessibility.
Practice
(1/5)
1. In Vue, how do you access the native event object inside an event handler method when using @click?
easy
A. By adding a parameter to the method and Vue passes the event automatically
B. You must import the event object manually
C. Use this.event inside the method
D. The event object is not accessible in Vue event handlers
Hint: Always receive event as parameter to use event methods [OK]
Common Mistakes:
Assuming event is globally available
Confusing template modifiers with method code
Using preventDefault without event parameter
5. You want to log the mouse coordinates when a user clicks a div in Vue. Which code correctly accesses the native event object and logs the coordinates?
hard
A. <div @click="logCoords">Click</div>\n<script setup>\nfunction logCoords(event) {\n console.log(event.clientX, event.clientY);\n}\n</script>
B. <div @click="logCoords($event)">Click</div>\n<script setup>\nfunction logCoords() {\n console.log(event.clientX, event.clientY);\n}\n</script>
C. <div @click="logCoords($event)">Click</div>\n<script setup>\nfunction logCoords(event) {\n console.log(event.pageX, event.pageY);\n}\n</script>
D. <div @click="logCoords">Click</div>\n<script setup>\nfunction logCoords() {\n console.log($event.clientX, $event.clientY);\n}\n</script>
Solution
Step 1: Confirm event object passing in template
To access the native event object in the method, you must pass $event explicitly in the template.
Step 2: Check method parameter and properties used
The method must accept event as a parameter and use valid mouse coordinate properties like pageX and pageY or clientX and clientY. <div @click="logCoords($event)">Click</div>\n<script setup>\nfunction logCoords(event) {\n console.log(event.pageX, event.pageY);\n}\n</script> correctly passes $event and uses pageX, pageY.
Final Answer:
Option C passes $event and logs pageX, pageY correctly -> Option C
Quick Check:
Pass $event and accept event param = C [OK]
Hint: Pass $event in template and accept event param in method [OK]
Common Mistakes:
Not passing $event in template
Using event without parameter
Trying to use $event inside script without passing
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.