In the React documentation, left-click triggers the @click
event by default, while right-click triggers the @contextmenu
event. As with vanilla JavaScript, you simply need to specify the function to be called when the event occurs.
Within the methods
section, functions are declared within an object structure, separated by commas instead of semicolons. You can refer to the provided example in the documentation for guidance on structuring these functions.
The code snippet displayed aligns with the Options API format, so I will present a code example following this pattern below.
Example with Options API
<template>
<div>
<button @click="(event) => handleClick(event)">Click Me</button>
</div>
</template>
<script>
export default {
methods: {
handleClick(event) {
// context.someFunction(); // not applicable here
this.someFunction(); // call methods.someFunction()
}, // <--- use , instead of ; as methods is an object containing functions like handleClick() and someFunction()
someFunction() {
// ...
}
}
}
</script>