当前位置:网站首页>微信小程序_19,自定义组件-behaviors
微信小程序_19,自定义组件-behaviors
2022-07-21 13:55:00 【icestone_kai】
什么是behaviors:
behaviors是小程序中,用于实现组件间代码共享的特性,类似于vue的mixins
behaviors的工作方式
每个behaviors可以包含一组属性,数据,生命周期函数和方法,组件引用它时,它的属性,数据和方法会被合并到组件中
每个组件可以引用多个behavior,behavior也可以引用其他的behavior
创建behavior:
调用behavior(Object object)方法即可一个共享的behavior实例对象,供所有的组件使用:
在下面创建对应文件夹和js文件:
module.exports = Behavior({
// 私有数据节点
data: {
},
// 属性节点
properties: {
},
// 事件处理函数和自定义方法节点
methods: {
}
})
导入并使用behaviors:
在组件中,使用require()方法导入需要的behavior,挂载后即可访问behavior中的数据或方法,示例代码如下:
// components/myTest1/myTest1.js
const myBehavior = require('../../behaviors/myBehavior1')
Component({
behaviors: [myBehavior],
})
behaviors挂载时,传入的是数组
使用:
例如这里在behavior中定义了一个data:
module.exports = Behavior({
// 私有数据节点
data: {
username: 'zs'
},
// 属性节点
properties: {
},
// 事件处理函数和自定义方法节点
methods: {
}
})
在引用了该behavior的组件的wxml中可以直接使用:
<text>
behaviors共享数据:{
{username}}
</text>
behavior中所有可用节点:
可用的节点 | 类型 | 是否必填 | 描述 |
---|---|---|---|
properties | Object Map | 否 | 通组件的属性 |
data | Object | 否 | 同组件的数据 |
methods | Object | 否 | 同自定义组件的方法 |
behaviors | String Array | 否 | 引入其它的behavior |
created | Function | 否 | 生命周期函数 |
attached | Function | 否 | 生命周期函数 |
ready | Function | 否 | 生命周期函数 |
moved | Function | 否 | 生命周期函数 |
detached | Function | 否 | 生命周期函数 |
其中最常用的有:properties
,data
,methods
,behaviors
behaviors中同名字段的覆盖和组合规则:
组件和它引用的behavior中可以包含同名的字段,此时可以参考如下3种同名时的处理规则:
1.同名的数据字段(data)
2.同名的属性(properties)或方法(methods)
3.同名的生命周期函数
关于详细的覆盖和组合规则,查看微信官方文档
比如在组件的data节点定义一个username为ls,在behavior的data中也定义一个username为ls,那么此时它会优先使用组件中的data
边栏推荐
- 专注跨境支付一体化服务 iPayLinks获《财资》3A亚洲奖
- C语言的文件操作
- StringUtils 和 String 方法一览
- 2022长三角工业自动化展会将于10月在南京国际展览中心召开
- [attack and defense world web] difficulty 1-star 3-point introductory questions: get, post, robots,, cookies, buttons, weak, PHP, web, serialize
- 2022亚洲国际物联网展会
- Write it down once Net analysis of CPU explosion of an intelligent transportation background service
- 申万宏源证券股票低佣金开户靠谱吗,可靠安全吗
- 理财产品到期不赎回会自动续期吗?
- 在代码评审中用好这7招,很容易就能建立起你的反对同盟
猜你喜欢
Get meituan, hungry, take out CPS and rights link
Postman - post request application/json parameter
Onvif协议及协议测试工具使用详解
ES6 from getting started to mastering 05: extended operators and arrow functions of functions
Detailed explanation of onvif protocol and protocol testing tools
idea报错Port 8080 is already in use
电脑是怎样上网的 (四) 局域网与服务器响应
聊聊接口设计的36个小技巧
pdf. JS how to Preview PDF files of Base64 type
Write it down once Net analysis of the memory leakage of the background service of a fire Internet of things
随机推荐
使用4D Nerf显示遮挡(CVPR2022)
pdf. JS how to Preview PDF files of Base64 type
ScePT: 为规划的场景一致和基于策略轨迹预测
半导体产业动态杂谈
92.(leaflet篇)leaflet态势标绘-进攻方向采集
Figure neural network: gat learning, understanding and pit entry
Tencent took out 38K two days ago, which showed me the basic ceiling. Today share is for you~
店铺那些违规会被官方二级处罚,最常见的4种
数组双指针-刻意练习
Postman - post request application / x-www-from-urlencoded
如何使用API接口批量查询图书信息?
[wechat applet authorization] obtain the user's mobile number and nickname
文件操作管理
File operation in C language
ES6 from getting started to mastering 02:let and const commands
写了几段小代码,把系统给搞崩了,被老板爆怼一顿
“中国网事·感动2022”二季度网络感动人物评选结果揭晓
C OPC client code
Ros2 learning notes: Launch script
DRDS 柔性事务漫谈