Home >WeChat Applet >Mini Program Development >A brief analysis of what are behaviors in mini programs? How to create and use it?
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!
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
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: { } })
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: { //... } //其他代码... })
properties,
data,
methods,
behaviors
Type | Required | Description | |
---|---|---|---|
Object Map | No | Properties of the same component | |
Object | No | Data of the same component | |
Object | No | Same as custom component methods | |
Sting Array | No | References other behaviors | |
Function | No | Lifecycle Function | |
Function | No | Life cycle function | |
Function | No | Life cycle function | |
Function | No | Life cycle function | |
Function | No | Life cycle function |
data)
properties) or methods (
methods) with the same name
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!