美文网首页
微信小程序如何在properties中处理属性值

微信小程序如何在properties中处理属性值

作者: JX灬君 | 来源:发表于2021-10-05 00:20 被阅读0次

如何在properties中处理属性值

  • 1.使用properties中的observer方法

  • 2.observer方法三个参数newVal, oldVal, changePath

  • 3.observer方法在properties中变量发生变化时,小程序开始调用

  • 4.千万不要在properties中修改自身属性值(容易形成无限递归,造成内存泄漏)

  • 4.使用方法(在data中定义一个变量用来映射需要处理的properties变量,通过observer方法返回需要的数据)

    // properties
    index : {
          type: Number,
          observer: function(newVal, oldVal, changePath){
            let val = newVal < 10 ? '0'+newVal : newVal
            this.setData({
              _index : val
            })
          }
    }
    // data
    data: {
        _index: 0
    },
    
  • 5.properties的observer方法的最优替代方法:wxs

相关文章

网友评论

      本文标题:微信小程序如何在properties中处理属性值

      本文链接:https://www.haomeiwen.com/subject/uxywnltx.html