on
Using the on method, you can register event handlers for target elements, making it easy to capture and respond to user interactions.
Here 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 attach a click event handler to the button element. When the user clicks the button, the handler is triggered, the counter increments, and the result is displayed on the page.
Template Syntax Usage
You can also use template syntax to bind methods to target elements.
{{count}}
In this example, we bind a method called addCount to the button element using on:click. When the user clicks the button, this method will be called, the counter's value will increment and be displayed on the page. This approach allows you to associate event handlers with component methods to implement more complex interactions.
event
After registering the event, the triggered function will receive the event argument, consistent with the native behavior.
-