In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how to create and use behaviors". The explanation in this article is simple and clear, easy to learn and understand. Please follow the ideas of Xiaobian and study and learn "how to create and use behaviors" together.
What are behaviors?
Behaviors are features in Mini programs that are used to implement component code sharing, similar to mixins in Vue.js. For example, in multiple components, there is a part of the code is exactly the same, we do not need to write each component again, for convenience, we can encapsulate this part of the code, put it in the behaviors for sharing, who uses this part of the code, direct reference can take effect
The way behaviors work
In Mini programs, each behavior can contain a set of properties, data, lifecycle, and methods. When a component references it, its properties, data, and methods are incorporated into the component.
Multiple behaviors can be referenced in each component, and each behavior can be referenced to each other.
Creation of behaviors
Calling the behaviors(Object Object) method creates a shared behaviors instance object for use by all components
//share behavior instance objects using module.exports module.exports = Behavior({ //private data node data: { }, //attribute node properties: { }, //Event Handling 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. Use `require()` to import the required custom behaviors module const myBehaviors = require("../../ behaviors/behaviors")Component({ //2. Mount the imported behaviors instance object into the node of behaviors array to use behaviors: [myBehaviors], properties: { //... } //Other codes...}) All available nodes in behaviors
Properties, data, methods, behaviors
Available node types Required Description propertiesObject Map No properties of the same component dataObject No data of the same component methodsObject No methods of the same custom component behaviorsSting Array No references to other behaviorscreatedFunction No lifecycle function attachedFunction No lifecycle function readyFunction No lifecycle function movedFunction No lifecycle function detachedFunction No lifecycle function overrides and combination rules for fields of the same name
Components and behaviors they refer to can contain fields with the same name. In this case, you can refer to the following three processing rules with the same name.
Data field with the same name (data)
If the data fields with the same name are all object types, object merging occurs.
The rest of the cases are overwritten, overriding rules: Component> Parent behaviors > Child behaviors, Lower behaviors > Upper behaviors (high priority overrides low priority, largest highest priority)
Properties or methods of the same name
If the component itself has this property or method, the component's property or method overrides the property or method of the same name in behaviors
If the component itself does not have this property or method, the property or method of the later behaviors defined in the component's behaviors field overrides the earlier property or method of the same name
Based on the previous rule, if there are nested behaviors, the rule is that parent behaviors override attributes or methods of the same name in child behaviors.
Life cycle function of the same name
behaviors take precedence over component execution
Child behaviors take precedence over parent behaviors
Top behaviors take precedence over bottom behaviors
Follow the order of component lifecycle functions between different lifecycle functions
For the same kind of life cycle function, follow the following rules
If the same behaviors are referenced multiple times by a component, it defines a lifecycle function that is executed only once.
Thank you for reading, the above is "how to create and use behaviors" content, after the study of this article, I believe we have a deeper understanding of how to create and use behaviors, the specific use of the situation also needs to be verified by practice. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.