<!-- When the custom component triggers the "myevent" event, the "onMyEvent" method is called --><component-tag-name bindmyevent="onMyEvent" /><!-- Alternatively, it can be written as --><component-tag-name bind:myevent="onMyEvent" />
Page({onMyEvent(e) {e.detail // Detail object provided when the custom component triggers an event.}})
<!-- Within the custom component --><button bindtap="onTap">Clicking this button will trigger the "myevent" event</button>
Component({properties: {},methods: {onTap() {const myEventDetail = {} // Detail object, provided for the event listener function.const myEventOption = {} // Options for triggering the event.this.triggerEvent('myevent', myEventDetail, myEventOption)}}})
Option Name | Type | Required or not | Default value | Description |
bubbles | Boolean | No | false | Does the event bubble? |
composed | Boolean | No | false | Does the event traverse component boundaries? When set to false, the event can only be triggered within the node tree of the referenced component, and does not penetrate into any other component. |
capturePhase | Boolean | No | false | Does the event have a capture phase? |
// Page page.wxml<another-component bindcustomevent="pageEventListener1"><my-component bindcustomevent="pageEventListener2"></my-component></another-component>
// Component another-component.wxml<view bindcustomevent="anotherEventListener"><slot /></view>
// Component custom-component.wxml<view bindcustomevent="myEventListener"><slot /></view>
// Component my-component.jsComponent({methods: {onTap() {this.triggerEvent('customevent', {}) // Will only trigger pageEventListener2.this.triggerEvent('customevent', {}, {bubbles: true}) // Will sequentially trigger pageEventListener2 and pageEventListener1.this.triggerEvent('customevent', {}, {bubbles: true, composed: true}) // Will sequentially trigger pageEventListener2, anotherEventListener, and pageEventListener1.}}})
Was this page helpful?