Home >WeChat Applet >Mini Program Development >A brief analysis of what are behaviors in mini programs? How to create and use it?

A brief analysis of what are behaviors in mini programs? How to create and use it?

青灯夜游
青灯夜游forward
2022-02-14 19:53:044982browse

What are behaviors? The following article will take you through the behaviors of custom components in mini programs, introduce how to create behaviors, import and use them, I hope it will be helpful to you!

A brief analysis of what are behaviors in mini programs? How to create and use it?

What are behaviors

behaviors are features used to share component code in mini programs, similar to those in Vue.js of mixins. For example, in multiple components, there is a part of the code that is exactly the same. We do not need to write it for each component. For convenience, we can encapsulate this part of the code and put it in behaviors for sharing. Who uses this part of the code? Direct quotation will take effect


How behaviors work

  • In the mini program, each behavior can contain a set of attributes, data, Lifecycle and methods. When a component references it, its properties, data, and methods are merged into the component.
  • Each component can reference multiple behaviors, and each behavior can also reference each other

Creation of behaviors

Call the behaviors(Object Object) method to create a shared behaviors instance object for use by all components

//使用 module.exports 将 behavior 实例对象共享出去
module.exports = Behavior({
    
    //私有数据节点
    data: { },
    
    //属性节点
    properties: { },
    
    //事件处理
    methods: {  }
})

Import and use of behaviors

In the component, use the require() method to import the required behaviors. After mounting, you can access the data or methods in the behaviors

//1.使用 `require()` 导入需要的自定义 behaviors 模块
const myBehaviors = require("../../behaviors/behaviors")

Component({

  //2. 将导入的 behaviors 实例对象,挂载到 behaviors数组的节点中便可以使用
  behaviors: [myBehaviors],
  
  properties: {
    //...
  }
  
  //其他代码...
})

# All available nodes in ##behaviors

The more commonly used ones are

properties, data, methods, behaviors

Available nodesTypeRequiredDescriptionpropertiesObject MapNoProperties of the same componentdata ObjectNoData of the same componentmethodsObject NoSame as custom component methodsbehaviorsSting ArrayNoReferences other behaviorscreatedFunctionNoLifecycle Functionattached FunctionNoLife cycle functionreadyFunctionNo Life cycle functionmovedFunctionNoLife cycle functiondetachedFunctionNoLife cycle function

Covering and combination rules for fields with the same name

Components and the behaviors they reference can contain fields with the same name. In this case, you can refer to the following three processing rules for the same name

  • Data fields with the same name (

    data)

      If the data fields with the same name are all object types, object merging will be performed
    • Other cases will be performed Data coverage, coverage rules: Components > Parent behaviors > Child behaviors, later behaviors > Front behaviors (higher priority overrides lower priority, the largest has the highest priority)
  • Properties (

    properties) or methods (methods) with the same name

      If the component itself has this property or method, then The properties or methods of the component will overwrite the properties or methods of the same name in behaviors
    • If the component itself does not have this property or method, the properties or methods of the later behaviors defined in the component's behaviors field will override the earlier ones. Attributes or methods with the same name
    • Based on the previous article, if there are nested references to behaviors, the rule is: parent behaviors override attributes or methods with the same name in child behaviors
  • Life cycle functions with the same name

      For different life cycle functions, follow the order of component life cycle functions
    • For the same type of life cycle functions, follow the following rules
      • Behaviors take precedence over component execution
      • Child behaviors take precedence over parent behaviors
      • Front behaviors take precedence over later behaviors
    • If the same behaviors are referenced multiple times by a component, it will define a life cycle function that will only be executed once
[Related learning recommendations:

Small program development Tutorial

The above is the detailed content of A brief analysis of what are behaviors in mini programs? How to create and use it?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:juejin.cn. If there is any infringement, please contact admin@php.cn delete