当前位置:网站首页>Wechat applet_ 19. Custom components -behaviors
Wechat applet_ 19. Custom components -behaviors
2022-07-22 05:56:00 【icestone_ kai】
What is? behaviors:
behaviors It's in the applet , Features for code sharing between components , Be similar to vue Of mixins
behaviors How it works
Every behaviors Can contain a set of properties , data , Life cycle functions and methods , When a component references it , Its properties , Data and methods are merged into components
Each component can reference more than one behavior,behavior You can also quote other behavior
establish behavior:
call behavior(Object object) Method is a shared behavior Instance object , For all components :
Create the corresponding folder and js file :
module.exports = Behavior({
// Private data node
data: {
},
// Attribute node
properties: {
},
// Event handler function and custom method node
methods: {
}
})
Import and use behaviors:
In components , Use require() Method to import the required behavior, Once mounted, you can access behavior Data or methods in , The sample code is as follows :
// components/myTest1/myTest1.js
const myBehavior = require('../../behaviors/myBehavior1')
Component({
behaviors: [myBehavior],
})
behaviors When mounting , What is passed in is an array
Use :
For example, here in behavior A is defined in data:
module.exports = Behavior({
// Private data node
data: {
username: 'zs'
},
// Attribute node
properties: {
},
// Event handler function and custom method node
methods: {
}
})
The behavior Of components of wxml Can be used directly in :
<text>
behaviors Shared data :{
{username}}
</text>
behavior All available nodes in :
Available nodes | type | If required | describe |
---|---|---|---|
properties | Object Map | no | Pass the properties of the component |
data | Object | no | Data of the same component |
methods | Object | no | The same as the method of custom components |
behaviors | String Array | no | Introduce other behavior |
created | Function | no | Life cycle function |
attached | Function | no | Life cycle function |
ready | Function | no | Life cycle function |
moved | Function | no | Life cycle function |
detached | Function | no | Life cycle function |
The most commonly used of these are :properties
,data
,methods
,behaviors
behaviors Overwrite and combination rules of fields with the same name in :
Component and what it references behavior Fields with the same name can be included in , At this point, you can refer to the following 3 A processing rule with the same name :
1. A data field with the same name (data)
2. Property with the same name (properties) Or method (methods)
3. Life cycle function with the same name
About detailed coverage and composition rules , Check the official wechat documents
For example, in the component data A node defines a username by ls, stay behavior Of data There is also a definition of username by ls, At this time, it will give priority to the data
边栏推荐
- 模电——电阻是什么?
- D3.js + Canvas 绘制组织结构图
- C # use objects comparer to compare objects
- Attack and defense world ---mfw
- 移动机器人(四)四轴飞行器
- 初步认识Redis
- MySQL: how are MySQL clients and servers connected?
- The wonderful problem encountered in FPGA design - "chip also depends on origin" (III)
- Detr code
- How to quickly import datatable into excel?
猜你喜欢
Tensorflow入门教程(四十)——ACUNET
The state Internet Information Office made a decision on the administrative punishment related to the network security review of didi Global Co., Ltd. in accordance with the law
Openai officially announced that dall-e will open its beta to 1million users
An idea of solving agile iteration of desktop application with applet Technology
Performance area: the more you know, the more you don't know
聊聊接口设计的36个小技巧
【如何优化她】教你如何定位不合理的SQL?并优化她~~~
半导体产业动态杂谈
2022长三角工业自动化展会将于10月在南京国际展览中心召开
Revit API:EditScope
随机推荐
Postman - post request application / x-www-from-urlencoded
互联网和传输层协议
Postman - post请求application/x-www-from-urlencoded
半导体产业动态杂谈
专注跨境支付一体化服务 iPayLinks获《财资》3A亚洲奖
92. (leaflet chapter) leaflet situation plotting - acquisition of attack direction
百度飞桨EasyDL X 韦士肯:看轴承质检如何装上“AI之眼”
Tensorflow入门教程(四十)——ACUNET
今天面了个腾讯拿 38K 出来的,让我见识到了基础的天花板
C语言解题——Number Sequence
【如何优化她】教你如何定位不合理的SQL?并优化她~~~
yii2的加密解密那些事儿
ssm框架文件上传已经实现怎么映射到数据库
DETR代码
牛客网刷题——第三天
The relevant person in charge of the state Internet Information Office answered reporters' questions on the decision to impose administrative penalties related to network security review on didi Globa
初步认识Redis
Talk about 36 tips of interface design
Explain soap Simple Object Access Protocol in detail
Matlab least square fitting