When WeChat Mini Programs acquires node information, why is it sometimes there and sometimes it is empty?

Custom component life cycle res.height sometimes reports to 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)

}

Feb.27,2021

may be a loading lifecycle problem.
try writing under onShow ()


your console.log must be written into the callback function, otherwise console will be executed before boundingClientRect triggers the callback and setData.

Menu