黑马程序员微信小程序学习总结8.数据监听器,生命周期

数据监听器

在这里插入图片描述

例子:
在这里插入图片描述

监听属性:
在这里插入图片描述

监听器案例

wxml:

<!--components/test3/test3.wxml-->
<text>components/test3/test3.wxml</text>
<view class="colorBox" style="background-color: rgb({{fullColor}});">颜色值:{{fullColor}}</view>
<button size="mini" type="default" bind:tap="setR">R</button>
<button size="mini" type="primary" bind:tap="setG">G</button>
<button size="mini" type="warn" bind:tap="setB">B</button>
<view>{{rgb.r}},{{rgb.g}},{{rgb.b}}</view>

wxss:

/* components/test3/test3.wxss */
/* 
 line-height: 200rpx;#数值行高
  height: 200rpx;#colorBox颜色框高
  color: white;#文字颜色
  background-color: blue;#文字默认背景,会被style覆盖
  text-align: center;#文字左右居中
  text-shadow: 0rpx 0rpx 2rpx black;#文字阴影
  font-size: 24px;#文字大小
*/
.colorBox {
  line-height: 200rpx;
  height: 200rpx;
  color: white;
  background-color: blue;
  text-align: center;
  text-shadow: 0rpx 0rpx 2rpx black;
  font-size: 24px;
}

js:

// components/test3/test3.js
Component({
  observers:{
    // 多个属性不好写,可以使用rgb.**通配符
    'rgb.r, rgb.g, rgb.b': function(r,g,b){
      this.setData({
        'fullColor': `${r}, ${g}, ${b}`//一层值可以不用单引号直接写fullColor也行。${r}, ${g}, 和 ${b} 是模板字符串的表达式部分,它们会被变量 r、g 和 b 的值所替代,并且最终结果会被转换成字符串。所以反引号 ` ` 用于创建包含动态内容的字符串,从而构造出 fullColor 字符串。
      })
    }
  },
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    rgb:{
      r:0,
      g:0,
      b:0
    },
    fullColor: '0, 0, 0'
  },

  /**
   * 组件的方法列表
   */
  methods: {
    setR(){
      this.setData({
        'rgb.r': this.data.rgb.r + 5 > 255? 255:this.data.rgb.r + 5
      });
    },
    setG(){
      this.setData({
        'rgb.g': this.data.rgb.g + 5 > 255? 255:this.data.rgb.g + 5
      });
    },
    setB(){
      this.setData({
        'rgb.b': this.data.rgb.b + 5 > 255? 255:this.data.rgb.b + 5
      });
    }
  }
})

json:

{
  "component": true,
  "usingComponents": {}
}

纯数字字段

在这里插入图片描述
如上个案例的rgb(去掉wxml中的最后一个view后)

如何使用

在这里插入图片描述
上个例子中:

在这里插入图片描述
注意:记得把用到rgb的地方改为_rgb

自定义组件的生命周期

就是在组件生命周期时执行有需要的函数
在这里插入图片描述
主要的:
在这里插入图片描述

如何使用:lifetimes节点

在这里插入图片描述
不推荐使用旧方法
在这里插入图片描述

组件所在页面的生命周期

如:组件所在页面显示时设置组件某些值

在这里插入图片描述

如何使用(pageLifetimes)

在这里插入图片描述
在这里插入图片描述

然后将随机设置rgb值的函数在show里面调用即可。

例子:
html:

<!--components/test3/test3.wxml-->
<view class="colorBox" style="background-color: rgb({{fullColor}});">颜色值:{{fullColor}}</view>
<button size="mini" type="default" bind:tap="setR">R</button>
<button size="mini" type="primary" bind:tap="setG">G</button>
<button size="mini" type="warn" bind:tap="setB">B</button>
<view>{{_rgb.r}},{{_rgb.g}},{{_rgb.b}}</view>

wxss:

/* components/test3/test3.wxss */
/* 
 line-height: 200rpx;#数值行高
  height: 200rpx;#colorBox颜色框高
  color: white;#文字颜色
  background-color: blue;#文字默认背景,会被style覆盖
  text-align: center;#文字左右居中
  text-shadow: 0rpx 0rpx 2rpx black;#文字阴影
  font-size: 24px;#文字大小
*/
.colorBox {
  line-height: 200rpx;
  height: 200rpx;
  color: white;
  background-color: blue;
  text-align: center;
  text-shadow: 0rpx 0rpx 2rpx black;
  font-size: 24px;
}

json:

{
  "component": true,
  "usingComponents": {}
}
// components/test3/test3.js
Component({
  pageLifetimes:{
    show(){
      this._randomColor()
      console.log("show")
    },
    hide(){
      console.log("hide")
    },
    resize(size){
      console.log("resize")
    }
  },
  lifetimes:{
    created(){
      console.log("~~~~~created")
    },
    attached(){
      console.log("~~~~~attached")
    },
    detached(){
      console.log("~~~~~detached")
    }
  },
  observers:{
    // 多个属性不好写,可以使用rgb.**通配符
    '_rgb.r, _rgb.g, _rgb.b': function(r,g,b){
      this.setData({
        'fullColor': `${r}, ${g}, ${b}`//一层值可以不用单引号直接写fullColor也行。${r}, ${g}, 和 ${b} 是模板字符串的表达式部分,它们会被变量 r、g 和 b 的值所替代,并且最终结果会被转换成字符串。所以反引号 ` ` 用于创建包含动态内容的字符串,从而构造出 fullColor 字符串。
      })
    }
  },
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    _rgb:{
      r:0,
      g:0,
      b:0
    },
    fullColor: '0, 0, 0'
  },

  /**
   * 组件的方法列表
   */
  methods: {
    _randomColor(){
      this.setData({
        _rgb: {
          // Math.random()生成一个0到1之间的随机浮点数,然后将其乘以256,这样得到的结果就是一个介于0到255.999...之间的数。接着,Math.floor()函数将这个数向下取整,得到一个介于0到255的整数。因此,这段代码生成的值的范围是0到255之间的整数。
          r: Math.floor(Math.random() * 256),
          g: Math.floor(Math.random() * 256),
          b: Math.floor(Math.random() * 256)
        }
      })
    },
    setR(){
      this.setData({
        '_rgb.r': this.data._rgb.r + 5 > 255? 255:this.data._rgb.r + 5
      });
    },
    setG(){
      this.setData({
        '_rgb.g': this.data._rgb.g + 5 > 255? 255:this.data._rgb.g + 5
      });
    },
    setB(){
      this.setData({
        '_rgb.b': this.data._rgb.b + 5 > 255? 255:this.data._rgb.b + 5
      });
    }
  }
})

效果图:
在这里插入图片描述
每次重新编译会刷新默认值

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

I Am Rex

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值