小程序组件中的监听事件
需求:
微信小程序中,如果进行使用了component级的组件的话,在一些情况下,父组件中使页面中的数据进行变化,子组件中
的数据不会一起变化,由此可以使用该方法
方法:
使用observers方法对会发生改变的数据进行监听 observers与data同级
使用:
observers: {
'mage'(mage) { // mage 父组件中传过来的可能会进行改变的数据 如果进行改变的话,就继续执行
let arr = [];
Array.from(this.properties.mage.newsTitle).forEach((item, i) => {
if (i < 26) {
arr.push(item)
}
})
// e.detail.value.totalAmountTax.join().replace(/,/g, "")
if (arr.length == 26) {
arr.push('...')
}
// console.log(arr);
this.data.stu = arr.join().replace(/,/g, "")
// console.log(this.data.stu, '5151515151515151');
this.setData({ // 把获取到的数据放在data下面的这部分在页面中进行展示的数据中
stu: this.data.stu
}) // 获取成功,一旦父组件中的内容出现更改,子组件中的数据同步进行,并且页面上一同更改
}
},