小程序自定义组件observer执行问题
发布于 6 年前 作者 xiena 2023 次浏览 来自 问答
  1. 为什么在执行到observer时,this.properties的值还包括data中的值,官方文档说的是data包含properties中的值

  2. 执行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)

    },
回到顶部