uni-app中vue3使用uni.createSelectorQuery().in(this)报错


需求:uni-app中获取页面中元素的位置

查询了官网,写法如下:

onMounted(() => {
  const query = uni.createSelectorQuery().in(this)
  query
    .select('.cart')
    .boundingClientRect((data) => {
      console.log(data, 'data')
    })
    .exec()
})

发现报错如下:


error.png

因为是vue3 项目,所以删除了 in(this) 发现还是获取不到数据

null.png

最终终于找到了解决方法:

import { ref, onMounted, getCurrentInstance } from 'vue'

// 获取页面实例对象
const instance = getCurrentInstance()
onMounted(() => {
  const query = uni.createSelectorQuery().in(instance)
  query
    .select('.cart')
    .boundingClientRect((data) => {
      console.log(data, 'data')
    })
    .exec()
})
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。