微信小程序自定義組件boundingClientRect獲取到的rect值爲null

解決辦法:

在自定義組件內獲取必須用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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章