美文网首页
uni-app中vue3使用uni.createSelector

uni-app中vue3使用uni.createSelector

作者: Goorln | 来源:发表于2023-11-16 23:24 被阅读0次

需求: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()
})

相关文章

网友评论

      本文标题:uni-app中vue3使用uni.createSelector

      本文链接:https://www.haomeiwen.com/subject/nfyywdtx.html