这个好像是深拷贝与浅拷贝的问题吧
小程序自定义组件observer执行问题为什么在执行到observer时,this.properties的值还包括data中的值,官方文档说的是data包含properties中的值 执行observer时应该是imgs变化时才执行,但是我在函数中改变this.data.imglist时也会执行observer [代码]properties: {[代码][代码] [代码][代码]imgs: {[代码][代码] [代码][代码]type: Array,[代码][代码] [代码][代码]value: [],[代码][代码] [代码][代码]observer: [代码][代码]function[代码] [代码](newval, oldval) {[代码][代码] [代码][代码]console.log([代码][代码]this[代码][代码].properties)[代码][代码] [代码][代码]this[代码][代码].setData({[代码][代码] [代码][代码]imglist: [代码][代码]this[代码][代码].properties.imgs[代码][代码] [代码][代码]})[代码][代码] [代码][代码]}[代码][代码] [代码][代码]},[代码][代码] [代码][代码]kngId: {[代码][代码] [代码][代码]type: String,[代码][代码] [代码][代码]value: [代码][代码]''[代码][代码],[代码][代码] [代码][代码]observer: [代码][代码]function[代码] [代码]() {[代码][代码] [代码][代码]console.log(12334455555)[代码][代码] [代码][代码]this[代码][代码].setData({[代码][代码] [代码][代码]kngId: [代码][代码]this[代码][代码].properties.kngId[代码][代码] [代码][代码]})[代码][代码] [代码][代码]}[代码][代码] [代码][代码]},[代码][代码] [代码][代码]},[代码] [代码] [代码][代码]data: {[代码][代码] [代码][代码]isLoading: [代码][代码]false[代码][代码],[代码][代码] [代码][代码]showSlideBar: [代码][代码]false[代码][代码],[代码][代码] [代码][代码]kngId: [代码][代码]''[代码][代码], [代码][代码]// 知识id[代码][代码] [代码][代码]timer: [代码][代码]null[代码][代码],[代码][代码] [代码][代码]imglist: [],[代码][代码] [代码][代码]currentIndex: 1, [代码][代码]// 当前页[代码][代码] [代码][代码]total: 0, [代码][代码]// 图片总数[代码][代码] [代码][代码]readpl: 0, [代码][代码]// 查看进度[代码][代码] [代码][代码]width: 0, [代码][代码]// 进度条宽度[代码][代码] [代码][代码]count: 0 [代码][代码]// 加载的图片个数[代码][代码] [代码][代码]},[代码][代码] [代码][代码]// 给图片数组增加默认图片[代码][代码] [代码][代码]addDefaultPics: [代码][代码]function[代码] [代码]() {[代码][代码] [代码][代码]this[代码][代码].data.imglist.forEach((item, key) => {[代码][代码] [代码][代码]// 首先显示前五张图片[代码][代码] [代码][代码]if[代码] [代码](key >= 5) {[代码][代码] [代码][代码]item.defaultImg = [代码][代码]'/images/default400.png'[代码][代码] [代码][代码]} [代码][代码]else[代码] [代码]{[代码][代码] [代码][代码]item.defaultImg = item.url[代码][代码] [代码][代码]}[代码][代码] [代码][代码]})[代码][代码] [代码][代码]this[代码][代码].setData({[代码][代码] [代码][代码]imglist: [代码][代码]this[代码][代码].data.imglist[代码][代码] [代码][代码]})[代码] [代码] console.log(this.properties)[代码] [代码] [代码][代码]},[代码]
2021-04-28