1. 什么是 behaviors

2. behaviors 的工作方式

3. 创建 behavior
调用 Behavior(Object object) 方法即可创建一个共享的 behavior 实例对象,供所有的组件使用:

4. 导入并使用 behavior


5. behavior 中所有可用的节点

6. 同名字段的覆盖和组合规则*

关于详细的覆盖和组合规则,大家可以参考微信小程序官方文档给出的说明:
https://developers.weixin.qq.com/miniprogram/dev/framework/custom-component/behaviors.html
附:官方文档-behaviors
behaviors 是用于组件间代码共享的特性,类似于一些编程语言中的 “mixins” 或 “traits”。
每个 behavior 可以包含一组属性、数据、生命周期函数和方法。组件引用它时,它的属性、数据和方法会被合并到组件中,生命周期函数也会在对应时机被调用。 每个组件可以引用多个 behavior ,behavior 也可以引用其它 behavior 。
详细的参数含义和使用请参考 Behavior 参考文档。
组件中使用
组件引用时,在 behaviors 定义段中将它们逐个列出即可。
代码示例:
在开发者工具中预览效果
// my-component.js
var myBehavior = require('my-behavior')
Component({behaviors: [myBehavior],properties: {myProperty: {type: String}},data: {myData: 'my-component-data'},created: function () {console.log('[my-component] created')},attached: function () { console.log('[my-component] attached')},ready: function () {console.log('[my-component] ready')},methods: {myMethod: function () {console.log('[my-component] log by myMethod')},}
})
在上例中, my-component 组件定义中加入了 my-behavior,
而 my-behavior 结构为:
// my-behavior.js
module.exports = Behavior({behaviors: [],properties: {myBehaviorProperty: {type: String}},data: {myBehaviorData: {}},attached: function(){},methods: {myBehaviorMethod: function(){}}
})
- 属性:
myBehaviorProperty - 数据字段:
myBehaviorData - 方法:
myBehaviorMethod - 生命周期函数:
attached、created、ready
这将使 my-component 最终结构为:
- 属性:
myBehaviorProperty、myProperty - 数据字段:
myBehaviorData、myData - 方法:
myBehaviorMethod、myMethod - 生命周期函数:
attached、created、ready
当组件触发生命周期时,上例生命周期函数执行顺序为:
[my-behavior] created[my-component] created[my-behavior] attached[my-component] attached[my-behavior] ready[my-component] ready
详细规则参考 同名字段的覆盖和组合规则。
同名字段的覆盖和组合规则
组件和它引用的 behavior 中可以包含同名的字段,对这些字段的处理方法如下:
- 如果有同名的属性 (properties) 或方法 (methods):
- 若组件本身有这个属性或方法,则组件的属性或方法会覆盖
behavior中的同名属性或方法; - 若组件本身无这个属性或方法,则在组件的
behaviors字段中定义靠后的behavior的属性或方法会覆盖靠前的同名属性或方法; - 在 2 的基础上,若存在嵌套引用
behavior的情况,则规则为:引用者 behavior覆盖被引用的 behavior中的同名属性或方法。
- 若组件本身有这个属性或方法,则组件的属性或方法会覆盖
- 如果有同名的数据字段 (data):
- 若同名的数据字段都是对象类型,会进行对象合并;
- 其余情况会进行数据覆盖,覆盖规则为:
引用者 behavior>被引用的 behavior、靠后的 behavior>靠前的 behavior。(优先级高的覆盖优先级低的,最大的为优先级最高)
- 生命周期函数和 observers 不会相互覆盖,而是在对应触发时机被逐个调用:
- 对于不同的生命周期函数之间,遵循组件生命周期函数的执行顺序;
- 对于同种生命周期函数和同字段 observers ,遵循如下规则:
behavior优先于组件执行;被引用的 behavior优先于引用者 behavior执行;靠前的 behavior优先于靠后的 behavior执行;
- 如果同一个
behavior被一个组件多次引用,它定义的生命周期函数和 observers 不会重复执行。
我的代码

<!--component/myComponent/myComponent.wxml-->
<text>component/myComponent/myComponent.wxml</text>
<text>components/my-component/my-component.wxml</text>
<view>我是my-component</view>
-----------------------不同----
<view>myComponent里的</view>
<button bindtap="myMethod"> myMethod</button>
<view>data数据--myData:{{myData}}</view>
<view>properties数据--myProperty:{{myProperty}}</view>
<view>Behavior里的</view>
<view>data数据--myBehaviorData:{{myBehaviorData}}</view>
<view>properties数据--myBehaviorProperty:{{myBehaviorProperty}}</view>
<button bindtap="myBehaviorMethod">myBehaviorMethod</button>
---------------------相同----------------------------
<view>相同的属性</view>
<view>strTest:{{strTest}}</view>
<view>相同的数据</view>
<view>myObject:{{myObject.username}}--{{myObject.age}}</view>
<view>相同的方法</view>
<button bindtap="commonMethod"> commonMethod</button>
// components/my-component/my-component.js
const myBehavior = require('../../behaviors/my-behaviors')
Component({behaviors: [myBehavior],properties: {myProperty: {type: String,value: 'myComponent'},strTest:{type: String,value: 'my-component'}},data: {myData: 'my-component-data',myObject:{username:'[my-component] zhangsan'}},created: function () {console.log('[my-component] created')},attached: function () { console.log('[my-component] attached')},ready: function () {console.log('[my-component] ready')},methods: {myMethod: function () {console.log('[my-component] log by myMethod')},commonMethod(){console.log("我是myComponent"); }}
})
// my-behaviors.js
module.exports = Behavior({behaviors: [],properties: {myBehaviorProperty: {type: String,value: 'myBehaviorProperty'},strTest:{type:String,value:'Behavior'}},data: {myBehaviorData: "myBehaviorData",myObject:{username:"张三",age:20}},created: function () {console.log('[behavior] created')},attached: function () { console.log('[behavior] attached')},ready: function () {console.log('[behavior] ready')},methods: {myBehaviorMethod: function(){console.log('myBehaviorMethod');},commonMethod(){console.log("我是behavior"); }}
})
结果



