微信小程序同一页面监听多个数据

    // 监听正确个数发送websocket
    app.watch(this, {
      checkNum: function (newVal) {
        let socketMsgQueue = {
          "userCode": app.globalData.userData.userCode,
          "userName": app.globalData.userData.userName,
          "blockCount": newVal,
          "pictureId": app.globalData.imageInfo.pictureId,
          "code": '2',
          "state": '0'
        }
        this.sendSocketMessage(socketMsgQueue)
        console.log('socketMsgQueue', socketMsgQueue)
      },
      // 监听图片url变化
      imgUrlLoading: function (newVal) {
        console.log(newVal)
        this.setData({
          imgUrl: newVal
        })
      },
      immediate: true
    })

app.js

  // 设置监听器
  watch: function (ctx, obj) {
    Object.keys(obj).forEach(key => {
      this.observer(ctx.data, key, ctx.data[key], function (value) {
        obj[key].call(ctx, value)
      })
    })
  },
  // 监听属性,并执行监听函数
  observer: function (data, key, val, fn) {
    Object.defineProperty(data, key, {
      configurable: true,
      enumerable: true,
      get: function () {
        return val
      },
      set: function (newVal) {
        if (newVal === val) return
        fn && fn(newVal)
        val = newVal
      },
    })
  },

猜你喜欢

转载自blog.csdn.net/weixin_51263829/article/details/128641333