【微信小程序】自定义组件 - behaviors

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 可以包含一组属性、数据、生命周期函数和方法。组件引用它时,它的属性、数据和方法会被合并到组件中,生命周期函数也会在对应时机被调用。 每个组件可以引用多个 behaviorbehavior 也可以引用其它 behavior

详细的参数含义和使用请参考 Behavior 参考文档

组件中使用

组件引用时,在 behaviors 定义段中将它们逐个列出即可。

代码示例:

在开发者工具中预览效果

js 复制代码
// 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 结构为:

ruby 复制代码
// my-behavior.js
module.exports = Behavior({
  behaviors: [],
  properties: {
    myBehaviorProperty: {
      type: String
    }
  },
  data: {
    myBehaviorData: {}
  },
  attached: function(){},
  methods: {
    myBehaviorMethod: function(){}
  }
})
  • 属性:myBehaviorProperty
  • 数据字段:myBehaviorData
  • 方法:myBehaviorMethod
  • 生命周期函数:attachedcreatedready

这将使 my-component 最终结构为:

  • 属性:myBehaviorPropertymyProperty
  • 数据字段:myBehaviorDatamyData
  • 方法:myBehaviorMethodmyMethod
  • 生命周期函数:attachedcreatedready

当组件触发生命周期时,上例生命周期函数执行顺序为:

  1. [my-behavior] created
  2. [my-component] created
  3. [my-behavior] attached
  4. [my-component] attached
  5. [my-behavior] ready
  6. [my-component] ready

详细规则参考 同名字段的覆盖和组合规则

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

组件和它引用的 behavior 中可以包含同名的字段,对这些字段的处理方法如下:

  • 如果有同名的属性 (properties) 或方法 (methods):
    1. 若组件本身有这个属性或方法,则组件的属性或方法会覆盖 behavior 中的同名属性或方法;
    2. 若组件本身无这个属性或方法,则在组件的 behaviors 字段中定义靠后的 behavior 的属性或方法会覆盖靠前的同名属性或方法;
    3. 在 2 的基础上,若存在嵌套引用 behavior 的情况,则规则为:引用者 behavior 覆盖 被引用的 behavior 中的同名属性或方法。
  • 如果有同名的数据字段 (data):
    • 若同名的数据字段都是对象类型,会进行对象合并;
    • 其余情况会进行数据覆盖,覆盖规则为: 引用者 behavior > 被引用的 behavior靠后的 behavior > 靠前的 behavior。(优先级高的覆盖优先级低的,最大的为优先级最高)
  • 生命周期函数和 observers 不会相互覆盖,而是在对应触发时机被逐个调用:
    • 对于不同的生命周期函数之间,遵循组件生命周期函数的执行顺序;
    • 对于同种生命周期函数和同字段 observers ,遵循如下规则:
      • behavior 优先于组件执行;
      • 被引用的 behavior 优先于 引用者 behavior 执行;
      • 靠前的 behavior 优先于 靠后的 behavior 执行;
    • 如果同一个 behavior 被一个组件多次引用,它定义的生命周期函数和 observers 不会重复执行。

我的代码

ruby 复制代码
<!--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>
ruby 复制代码
// 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"); 
    }
  }
})
ruby 复制代码
// 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"); 
    }
  }
})

结果



相关推荐
云起SAAS10 小时前
B2B 木材行业供需对接平台微信小程序开源
微信小程序·小程序·ai编程·看广告变现轻·b2b 木材行业供需对接平台
程序媛徐师姐12 小时前
Java基于微信小程序的球馆预约系统,附源码+文档说明
java·微信小程序·球馆预约系统小程序·jav球馆预约系统小程序·java球馆预约微信小程序·球馆预约微信小程序·java球馆预约系统
毕设源码-邱学长12 小时前
【开题答辩全过程】以 基于微信小程序地方小吃分享平台设计与实现为例,包含答辩的问题和答案
微信小程序·小程序
不懂代码的切图仔12 小时前
小程序web-view嵌入h5扫码 jssdk方式
前端·微信小程序
BugShare14 小时前
小程序构建npm时报错应该如何解决
微信小程序·npm
大尚来也16 小时前
自助建站系统有哪些?自助建站平台深度对比
微信小程序
码云数智-园园16 小时前
2026建网站一般需要多少钱?
微信小程序
嫂子开门我是_我哥19 小时前
从零开发微信小程序+若依后端项目:本地全流程开发,从环境搭建到前后端联调跑通
微信小程序·小程序·若依
Kingexpand_com2 天前
物联网APP开发实战:如何打造用户真正愿意用的智能硬件伴侣
物联网·小程序·app·智能硬件·物联网app定制开发
CHU7290352 天前
家政同城服务APP前端功能玩法解析
前端·小程序