微信小程序自定义组件boundingClientRect获取到的rect值为null

7次阅读

共计 389 个字符,预计需要花费 1 分钟才能阅读完成。

解决办法:

在自定义组件内获取必须用 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

正文完
 0