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()
搞定