解決辦法:
在自定義組件內獲取必須用SelectorQuery.in()
Component({
lifetimes: {
ready() {
const query = wx.createSelectorQuery().in(this)
const num = Math.ceil(this.data.picList.length / LINE_LENGTH)
query.select('.tab-content-item').boundingClientRect((rect) => {
this.setData({
swiperHeight: rect.height * num + 'rpx'
})
}).exec()
}
},
})
const query = wx.createSelectorQuery().in(this)
這一句是最重要的,要用.in(this),this傳入的是自定義組件的實例。
否則獲取到的rect值爲null
https://segmentfault.com/a/1190000019548718?utm_source=tag-newest&tdsourcetag=s_pcqq_aiomsg