Vue3版本中 在自定义组件中使用uni.createSelectorQuery() 方式 .in(this)无法实现
在组件内部获取 getCurrentInstance() 返回null 所以无法获取到节点信息, 该如何实现在组件内获取节点信息呢

1***@qq.com
- 发布:2023-04-24 21:06
- 更新:2025-08-07 17:31
- 阅读:1507
4 个回复
1***@qq.com - like
同问
1***@qq.com - like
找到了解决办法:需要在组件上定义ref,然后用
uni.createSelectorQuery().in(compRef.value)
2***@qq.com
为什么我定义了const instance = ref(null),最后instance.value也为null呢?
2023-07-26 00:34
天天搬砖
回复 2***@qq.com: 解决了吗?
2023-08-22 08:53
肖建军
我试了,不行啊
2024-04-05 20:50
2***@qq.com
把getCurrentInstance() 写在setup下
飞仙_l
可以参考一下
import { ref, reactive, onMounted, computed, onUnmounted, nextTick, getCurrentInstance } from 'vue';
export function useDomConfig() {
const instance: any = getCurrentInstance();
const getDomMessage = (dom: any, callback: Function) => {
nextTick(() => {
const query = uni.createSelectorQuery().in(instance.proxy);
query
.select(dom)
.boundingClientRect((data) => {
callback(data);
})
.exec();
});
};
}