Vue3 + Vite + Ts 获取dom(通过ref)
元素上ref和 Vue2 一样:<div class="classfy_cell flex j-a a-c" ref="classfy">获取dom<script setup lang="ts">import { getCurrentInstance, onMounted } from "vue"; // 引入全局let refs = null;onMounted(() =
·
元素上ref和 Vue2 一样:
<div class="classfy_cell flex j-a a-c" ref="classfy">
获取dom
<script setup lang="ts">
import { getCurrentInstance, onMounted } from "vue"; // 引入全局
let refs = null;
onMounted(() => {
let { $refs } = (getCurrentInstance() as any).proxy;
refs = $refs;
console.log(refs.classfy);
});
</script>
这样就可以通过 refs 来操作dom
注意:
不能直接在 onMounted 外获取 $refs 得出的结果是 {},什么也没有,而且即使使用 nextTick 也是没用的,而且这个还会报错
最近发现还有更简便的方式获取 Dom :Vue3 + Vite + Ts 关于ref和v-model对应的字段的感悟_龙雨LongYu12的博客-CSDN博客
更多推荐
已为社区贡献5条内容
所有评论(0)