1.在自定义组件中使用wx.createSelectorQuery
const query = wx.createSelectorQuery().in(this)
query.select('#the-id').boundingClientRect(res => {
console.log(res)
}).exec()
预期是ready生命周期中,在子组件获取元素高度
如果不使用in(this), 获取的rect是null.但是使用this的话会
An SelectorQuery call is ignored because no proper page or component is found.
Please considering using SelectorQuery.in to specify a proper one.
精华
this 拿到是是 wepy 实例, this.$wx 才是小程序的实例
const query = wx.createSelectorQuery().in(this.$wx)
query.select('#the-id').boundingClientRect(res => {
console.log(res)
}).exec()
搞定