收藏
回答

使用 this.setData赋值失败?

onReady: function () {
    let that = this
    wx.getSystemInfo({
      complete: (res) => {
        var query = wx.createSelectorQuery()
        query.select('#bottom').boundingClientRect(function(qRes) {
          that.setData({
            userInfoCardHeight:res.windowHeight-qRes.height
          })
          console.log(res.windowHeight-qRes.height)
        }).exec();
      },
    })
    console.log("用户卡片高度"+that.data.userInfoCardHeight)
  },
回答关注问题邀请回答
收藏

4 个回答

  • 睡前原谅一切
    睡前原谅一切
    2019-12-31
    同步异步问题,setData是异步的。像你那样写setData还没赋值完就先执行了打印,肯定是空的咯。改为这样。
    that.setData({
       userInfoCardHeight:res.windowHeight-qRes.height
    }, ()=> {
      console.log(that.data.userInfoCardHeight)
    })
    
    2019-12-31
    有用 3
    回复 2
    • 张硕
      张硕
      2019-12-31
      谢谢谢谢
      2019-12-31
      回复
    • 睡前原谅一切
      睡前原谅一切
      2019-12-31回复张硕
      客气,
      2019-12-31
      回复
  • 小肥羊🍊
    小肥羊🍊
    2019-12-31

    setData函数用于将数据从逻辑层发送到视图层(异步),同时改变对应的this.data的值(同步)。



    https://developers.weixin.qq.com/miniprogram/dev/reference/api/Page.html#Page.prototype.setData(Object%20data,%20Function%20callback)

    2019-12-31
    有用 1
    回复
  • Once Was
    Once Was
    2019-12-31
    console.log("用户卡片高度"+that.data.userInfoCardHeight)这一句放在方法体里面
    
    2019-12-31
    有用 1
    回复
  • 龙
    2019-12-31

    不是赋值失败,是异步执行的问题,看一下控制台的打印顺序就知道了

    2019-12-31
    有用 1
    回复
登录 后发表内容
问题标签