Performance: Accessing the native event object
This affects input responsiveness and interaction speed by controlling how event data is accessed and handled in Vue components.
<template> <button @click="handleClick">Click me</button> </template> <script setup> function handleClick(event) { event.preventDefault(); doSomething(); } </script>
<template>
<button @click="$event.preventDefault(); doSomething()">Click me</button>
</template>| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Inline event handler with $event | No extra DOM nodes | 0 | 0 | [!] OK |
| Named method handling native event | No extra DOM nodes | 0 | 0 | [OK] Good |
| .native modifier on custom component | Adds extra event listener | 0 | 0 | [X] Bad |
| Emitting event from child component | No extra event listeners | 0 | 0 | [OK] Good |
@click?event to your method, you can access the native event details.$event inside templates to represent the native event object.$event explicitly to the method is the correct way to access the native event in inline handlers.<template>
<button @click="logEvent($event)">Click me</button>
</template>
<script setup>
function logEvent(event) {
console.log(event.type);
}
</script>$event is the native event object for the click event on the button.event.type returns the type of the event, which is "click" for a click event.<template>
<form @submit="submitForm">
<button type="submit">Submit</button>
</form>
</template>
<script setup>
function submitForm() {
event.preventDefault();
alert('Form submitted');
}
</script>event.preventDefault() but does not receive event as a parameter, so event is undefined.@submit.prevent modifier in the template.$event explicitly in the template.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.Pass the test and this pattern moves unknown