vue3 使用ref 获取 dom 元素的高度

代码实现:

<template>
  <div ref="mains" class="search"></div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
const mains = ref()

onMounted(() => {
  const height = mains.value.clientHeight
  console.log(height, 1234)
})
</script>

<style scoped lang="less">
.search {
  width: 100px;
  height: 100px;
  margin: 0 auto;
  background-color: greenyellow;
}
</style>

输出结果:

猜你喜欢

转载自blog.csdn.net/qq_44694453/article/details/129030752