微信小程序获取节点信息,为什么有的时候有,有的时候就是空的?

自定义组件生命周期res.height有的时候报null

ready: function () {

var that = this
var query = wx.createSelectorQuery().in(this)
query.select('.question').boundingClientRect(function (res) {
  that.setData({
    questionHeight: res.height
  })
}).exec()
// console.log(that.data.questionHeight)

}

阅读 3.2k
2 个回答

可能是加载的生命周期问题。
写在onShow()下试试

你的console.log得写到回调函数里面,不然会出现boundingClientRect还未触发回调、并setData的操作,而先执行了console。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题