The format of event binding is similar to that of component attributes.
- The key starts with bind or catch, followed by the event type, for example, bindtap or catchtouchstart. Also, bind and catch can be followed by a colon, for example, bind:tap and catch:touchstart. The functions of these two formats are the same.
- The value is a string. You need to define a function with the same name on the corresponding Page. Otherwise, an error is reported when an event is triggered.
- The difference between using bind and catch is that binding events does not prevent event bubbling, while catching event does.
Example:
<view id="view1" bindtap="handleTap1">
view1
<view id="view2" catchtap="handleTap2">
view2
<view id="view3" bindtap="handleTap3">
view3
</view>
</view>
</view> Effect:
- When you tap view3, handleTap3 and handleTap2 are called in sequence. This is because the tap event bubbles to view2, but view2 prevents the tap event from bubbling and does not pass it to the parent node view1.
- When view2 is tapped, only handleTap2 is triggered.
- When view1 is tapped, only handleTap1 is triggered.