<custom-ul><custom-li>item 1</custom-li><custom-li>item 2</custom-li></custom-ul>
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.// path/to/custom-ul.jsComponent({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.jsComponent({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.}}}})
<custom-form><view>input<custom-input></custom-input></view><custom-submit>submit</custom-submit></custom-form>
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.jsmodule.exports = Behavior({// ...})
// path/to/custom-input.jsconst 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.jsconst 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.}}})
relations
definition, this behavior can be used in place of the component path as the target node for association:// path/to/custom-form.jsconst 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 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. |
Was this page helpful?