# Component(Object object)

Create a custom component that accepts a Object Parameter of type.

# parameter

# Object object

Definition segment type Is it necessary to fill out describe Minimum version
properties Object Map no External properties of a component are a map of property names to property settings
data Object no Component's internal data, and properties Template rendering for components together
observers Object no Component data field listener for listening properties and data See the changes in Data listener 2.6.1
methods Object no Component, including event response functions and arbitrary custom methods. For the use of event response function, see Inter-component communication and events
behaviors String Array no Inter-component code reuse mechanisms similar to mixins and traits, see behaviors
created Function no Component lifecycle function - executes when the component instance has just been created, noting that it cannot be called at this time setData )
attached Function no Component life cycle function - executed when component instance enters page node tree
ready Function no Component Life Cycle Function - Executed after component layout is complete)
moved Function no Component Lifecycle Function - Executes when the component instance is moved to another location in the node tree
detached Function no Component Lifecycle Function - Executes when component instance is removed from page node tree
relations Object no Relationship definitions between components, see Inter-component relations
externalClasses String Array no External style classes accepted by the component, see External style class
options Object Map no Some options (specific option settings are covered when the features are described in the documentation, not listed here)
lifetimes Object no Component lifecycle declaration object, see Component life cycle 2.2.3
pageLifetimes Object no The lifecycle declaration object for the page on which the component resides, see Component life cycle 2.2.3
definitionFilter Function no Defines segment filters for custom component extensions, see Custom component extension 2.2.3

Generated component instances can be found in component methods, lifecycle functions, and properties observer Through this Access. Components contain common properties and methods.

Attribute name type describe
is String Component file path
id String Node id
dataset String Node dataset
data Object Component data,Including internal data and attribute values
properties Object Component data,Including internal data and attribute values( data Consistent)
Method name parameter describe Minimum version
setData Object newData Set up data and perform view-layer rendering
hasBehavior Object behavior Check whether the component has behavior All behaviors that are introduced directly or indirectly are recursively checked.
triggerEvent String name, Object detail, Object options Trigger events, see Inter-component communication and events
createSelectorQuery Create a SelectorQuery Object, the selector selects from within this component instance
createIntersectionObserver Create a IntersectionObserver Object, the selector selects from within this component instance
createMediaQueryObserver Create a MediaQueryObserver object 2.11.1
SelecComponent String selector Select the component instance node using the selector and return the first component instance object that matches wx://component-export Impact)
selectAllComponents String selector Select the component instance node using the selector and return an array of matched component instance objects wx://component-export Impact)
selectOwnerComponent Selects the component instance of the current component node (that is, the reference to the component) and returns its component instance object wx://component-export Impact) 2.8.2
getRelationNodes String relationKey Gets all the associated nodes corresponding to this relationship, see Inter-component relations
groupSetData Function callback Immediate execution callback , many of which setData Interface drawing will not be triggered (only needed for special scenarios, such as for use in different components at the same time setData Interface rendering synchronization at 2.4.0
getTabBar Returns the current page custom-tab-bar For example, see[custom tabBar]((ability/custom tabs)) 2.6.2
getPageId Returns the page identifier (a string) that can be used to determine whether several custom component instances are in the same page 2.7.1
animate String selector, Array keyframes, Number duration, Function callback Execute keyframe animation, seeanimation 2.9.0
clearAnimation String selector, Object options, Function callback Clear keyframe animations, seeanimation 2.9.0
setUpdatePerformanceListener Object options, Function listener Clear keyframe animations, seeanimation 2.12.0

Code example:

Preview with Developer Tool

Component({

  behaviors: [],

  // Property definition (see below for details)
  properties: {
    myProperty: { // Attribute name 
      type: String,
      value: ''
    },
    myProperty2: String // Simplified definition
  },

  data:  {}, // Private data, available for template rendering

  lifetimes: {
    // Life cycle function, can be a function, or a method name defined in the methods section
    attached: function () { },
    moved: function () { },
    detached: function () { },
  },

  // Life cycle function, can be a function, or a method name defined in the methods section
  attached: function () { }, // The attached declaration here is overwritten by the declaration in the lifetimes field
  ready: function() { },

  pageLifetimes: {
    // Life Cycle Function for the Page on which the component is located
    show: function () { },
    hide: function () { },
    resize: function () { },
  },

  methods: {
    onMyButtonTap: function(){
      this.setData({
        // The method of updating properties and data is similar to that of updating page data
      })
    },
    // The internal method recommends starting with an underscore
    _myPrivateMethod: function(){
      // Here will be data.A[0].B Set to 'myPrivateData'
      this.setData({
        'A[0].B': 'myPrivateData'
      })
    },
    _propertyChange: function(newVal, oldVal) {

    }
  }

})

Note: in properties In the definition section, the property name is written as a humppropertyName)in wxml Property values are specified using the hyphen notationcomponent-tag-name property-name="attr value"Is applied to data binding in a hump notationattr="")。

# properties definition

Definition segment type Is it necessary to fill out describe Minimum version
type yes Type of attribute
optionalTypes Array no Property type (you can specify more than one) 2.6.5
value no The initial value of the
observer Function no Callback function when attribute value changes

Property values can be changed using the observer To monitor. Currently, this field is not recommended in the new version of the base library; instead, use the Component Constructor observers Field, which is more powerful and better performance.

Code example:

Component({
  properties: {
    min: {
      type: Number,
      value: 0
    },
    min: {
      type: Number,
      value: 0,
      observer: function(newVal, oldVal) {
        // When property values change
      }
    },
    lastLeaf: {
      // This property can be Number 、 String 、 Boolean One of three types.
      type: Number,
      optionalTypes: [String, Object],
      value: 0
    }
  }
})

Property can be of type String Number Boolean Object Array One, can also be null Represents an unrestricted type.

In most cases, it is best to specify an exact type for a property. So, in... WXML When an attribute value is specified literarily in, the value can obtain an exact type, such as

<custom-comp min="1" max="5" />

At this point, because the corresponding property of the custom component is specified as Number Type, min and Max Is going to be assigned to 1 and 5 Instead of "1" and "5" , namely:

this.data.min === 1 // true
this.data.Max === 5 // true

# Bug & Tips:

  • Use this.data Can get internal data and attribute valuesBut modifying it directly does not apply changes to the interface, and you should use the setData Modify.
  • Lifecycle functions cannot be passed through component methods this Access to.
  • Property names should be avoided with data Beginning, that is, do not name dataXyz This form, because in WXML In, data-xyz="" Will be used as nodes dataset Instead of component properties.
  • In the definition and use of a component, the component's attribute name and data Fields cannot conflict with each other (although they are in different definition segments).
  • From base library 2.0.9 To start, the properties of the object type and data A field can contain subfields of a function type, that is, a function can be passed through a property field of an object type. Base libraries below this release do not support this feature.
  • bug : Be located slot The custom component in the pageLifetimes The page lifecycle declared in the 2.5.2 Fix in.
  • bug : for type for Object or Array Property of the component, if passed by the component's own this.setData To change a subfield of the property value, the property will still be triggered observer And, observer Received newVal Is the value of the changed subfield, oldVal Is empty, changedPath Field name information containing child fieldsCurrently recommended observers Define paragraph instead.