1. 给dom元素添加ref=“”
  2. 添加响应式变量,这个变量的名字要和ref命名一致
<template>
  <!--ref-->
  <div class="demo" ref="dom"></div>
</template>

<script setup lang="ts">
import { onMounted, ref } from "vue";

// 获取单个dom
const dom = ref<HTMLElement | null>(null);

onMounted(() => {
  //获取div高度
  console.log((dom.value as HTMLElement).clientHeight);
});
</script>

<style scoped>
.demo {
  height: 500px;
}
</style>
Logo

前往低代码交流专区

更多推荐