微信小程序如何在properties中处理属性值
2021-10-05 本文已影响0人
JX灬君
如何在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