【微信小程序】自定义组件 - 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"); 
    }
  }
})

结果



相关推荐
毕设木哥7 小时前
25届计算机毕业设计选题推荐-图书馆智能选座系统
java·spring boot·微信小程序·小程序·毕业设计·课程设计
程序员入门进阶9 小时前
优购电商小程序的设计与实现+ssm(lw+演示+源码+运行)
小程序
人工智能的苟富贵11 小时前
微信小程序中实现类似于 ECharts 的图表渲染及优化
微信小程序·小程序·echarts
一 乐17 小时前
英语学习交流平台|基于java的英语学习交流平台系统小程序(源码+数据库+文档)
java·数据库·vue.js·学习·小程序·源码
Java Fans19 小时前
微信小程序页面制作——婚礼邀请函(含代码)
微信小程序·小程序
计算机学姐1 天前
基于微信小程序的高校实验室管理系统的设计与实现
java·vue.js·spring boot·mysql·微信小程序·小程序·intellij-idea
程序员入门进阶1 天前
基于小程序的教学辅助微信小程序设计+ssm(lw+演示+源码+运行)
微信小程序·小程序
我非夏日1 天前
小程序开发设计-第一个小程序:创建小程序项目④
小程序
前端 贾公子1 天前
微信小程序----日期时间选择器(自定义时间&&精确到分秒)
微信小程序·小程序
CV_CodeMan1 天前
uniapp组件uni-datetime-picker选择年月后在ios上日期不显示
前端·ios·小程序·uni-app