tencent cloud

Feedback

Component Events

Last updated: 2024-03-04 23:13:59

    Inter-component Communication

    There are several fundamental methods of communication between components.
    WXML data binding: This is used for setting data to a specific attribute of a child component from a parent component. It can only set JSON-compatible data initially, but it can also include functions within the data. For more details, see Component Templates and Styles.
    Events: Events are used for transmitting data from child components to parent components, and can convey any type of data.
    Should the aforementioned methods prove insufficient, the parent component can also obtain the child component instance object through the this.selectComponent method, thereby gaining direct access to any data and methods of the component.

    Monitor events

    The event system is one of the primary methods of communication between components. Custom components can trigger any event, and the page referencing the component can monitor these events. For basic concepts and usage of events, see WXML - Events.
    The method for monitoring custom component events is entirely consistent with the method for monitoring basic component events.
    Sample code:
    <!-- 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.
    }
    })

    Trigger event

    When a custom component triggers an event, the triggerEvent method must be used, specifying the event name, detail object, and event options:
    <!-- 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)
    }
    }
    })
    The options for triggering the event include:
    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?
    For concepts regarding the bubbling and capturing phases, see the relevant explanations in the WXML - Events section.
    // 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.js
    Component({
    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.
    }
    }
    })
    
    Contact Us

    Contact our sales team or business advisors to help your business.

    Technical Support

    Open a ticket if you're looking for further assistance. Our Ticket is 7x24 avaliable.

    7x24 Phone Support