on
Using the on method, you can register event handlers for target elements. This allows you to easily capture and respond to user interactions.
Below is an example demonstrating how to use the on method to register a click event handler for a button element:
-
In this example, we use the on method to add a click event handler to the button element. When the user clicks the button, the event handler is triggered, the counter increments, and the result is displayed on the page.
Using Template Syntax
You can also use template syntax to bind methods to target elements.
{{count}}
In this example, we use on:click on a button element to bind a method named addCount. When the user clicks the button, this method will be called, the counter value will increment and be displayed on the page. This approach allows you to associate event handlers with component methods, enabling more complex interactions.
event
After registering an event, the triggered function will be passed an event parameter, consistent with the native behavior:
-