小程序组件中wx.createSelectorQuery().select()无效结果为null

发布时间 2023-04-13 15:16:48作者: 大熊丨rapper

非组件页面:

 

wx.createSelectorQuery().in(this).select('.scroll-list').boundingClientRect((rect)=>{
            console.log(rect)
            this.setData({scrollViewWidth:Math.round(rect.width)}) // scroll-view元素长度
    
    }).exec()

 

 

组件页面:

1. 在组件在视图层布局完成后执行,即要在ready函数中操作  2. wx.createSelectorQuery().in(this).select('#the-id')  3. 官方文档:https://developers.weixin.qq.com/miniprogram/dev/api/wxml/SelectorQuery.in.html

// 组件生命周期函数-在组件布局完成后执行
  ready () {
    wx.createSelectorQuery().in(this).select('.scroll-list').boundingClientRect((rect)=>{
        console.log(rect)
        this.setData({scrollViewWidth:Math.round(rect.width)}) // scroll-view元素长度
        
    }).exec()
  },