tencent cloud

Feedback

Inter-component relationship

Last updated: 2024-03-04 23:14:35

    Define and use inter-component relationships

    At times, there is a need to implement such components:
    <custom-ul>
    <custom-li>item 1</custom-li>
    <custom-li>item 2</custom-li>
    </custom-ul>
    In this example, both custom-ul and custom-li are custom components, possessing intricate interrelationships. Communication between them can often be complex. By incorporating a relations definition section during component definition, such issues can be resolved.
    Sample code:
    // path/to/custom-ul.js
    Component({
    relations: {
    './custom-li': {
    type: 'child', // The target node in the relationship should be a child node.
    linked(target) {
    // Executed each time a custom-li is inserted, where target is the instance object of the node, triggered after the attached lifecycle of the node.
    },
    linkChanged(target) {
    // Executed each time a custom-li is moved, where target is the instance object of the node, triggered after the moved lifecycle of the node.
    },
    unlinked(target) {
    // Executed each time a custom-li is removed, where target is the instance object of the node, triggered after the detached lifecycle of the node.
    }
    }
    },
    methods: {
    _getAllLi() {
    // Using getRelationNodes, you can obtain an ordered array of nodes, which includes all associated custom-li.
    const nodes = this.getRelationNodes('path/to/custom-li')
    }
    },
    ready() {
    this._getAllLi()
    }
    })
    // path/to/custom-li.js
    Component({
    relations: {
    './custom-ul': {
    type: 'parent', // The target node in the relationship should be a parent node.
    linked(target) {
    // Executed each time it is inserted into custom-ul, where target is the instance object of the custom-ul node, triggered after the attached lifecycle.
    },
    linkChanged(target) {
    // Executed each time it is moved, where target is the instance object of the custom-ul node, triggered after the moved lifecycle.
    },
    unlinked(target) {
    // Executed each time it is removed, where target is the instance object of the custom-ul node, triggered after the detached lifecycle.
    }
    }
    }
    })
    Note:
    Both component definitions must include the relations definition, otherwise it will not take effect.

    Associate with a category of components

    To associate a category of components, see the following example code:
    <custom-form>
    <view>
    input
    <custom-input></custom-input>
    </view>
    <custom-submit>submit</custom-submit>
    </custom-form>
    The custom-form component aims to associate with two components: custom-input and custom-submit. At this point, if both of these components have the same behavior:
    // path/to/custom-form-controls.js
    module.exports = Behavior({
    // ...
    })
    // path/to/custom-input.js
    const customFormControls = require('./custom-form-controls')
    Component({
    behaviors: [customFormControls],
    relations: {
    './custom-form': {
    type: 'ancestor', // The target node to be associated with should be an ancestor node.
    }
    }
    })
    // path/to/custom-submit.js
    const customFormControls = require('./custom-form-controls')
    Component({
    behaviors: [customFormControls],
    relations: {
    './custom-form': {
    type: 'ancestor', // The target node to be associated with should be an ancestor node.
    }
    }
    })
    In the relations definition, this behavior can be used in place of the component path as the target node for association:
    // path/to/custom-form.js
    const customFormControls = require('./custom-form-controls')
    Component({
    relations: {
    customFormControls: {
    type: 'descendant', // The target node to be associates with should be a descendant node.
    target: customFormControls
    }
    }
    })

    Relations definition section

    The relations definition section includes the target component path and its corresponding options. The options that can be included are detailed in the table below.
    Option
    Type
    Required or not
    Description
    type
    string
    Yes
    The relative relationship of the target component, with the optional values of parent, child, ancestor, descendant.
    linked
    function
    No
    Relationship lifecycle function, triggered when the relationship is established in the page node tree and after the component's attached lifecycle.
    linkChanged
    function
    No
    Relationship lifecycle function, triggered when the relationship changes within the page node tree and after the component's moved lifecycle.
    unlinked
    function
    No
    Relationship lifecycle function, triggered when the relationship is detached from the page node tree and after the component's detached lifecycle.
    target
    string
    No
    If this item is set, it signifies the behavior that the associated target node should possess. All component nodes possessing this behavior will be associated with.
    
    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