Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0613· openaiHugging Face Suzhen/CodeChat

vue3 如何获取页面元素的饿高度

  1. User

    vue3 如何获取页面元素的饿高度

  2. Assistant

    在 Vue 3 中,可以使用 ref 来引用页面元素,然后通过 value 属性来获取元素的高度。

    首先,在页面元素上添加 ref 属性,并赋值给一个变量,如:

    <template>
      <div ref="myElement">Hello World</div>
    </template>
    

    然后,在 Vue 组件中引入 ref,并通过 value 属性获取元素的高度,如:

    <script>
    import { ref, onMounted } from 'vue';
    
    export default {
      setup() {
        const myElement = ref(null);
    
        onMounted(() => {
          // 获取元素的高度
          const height = myElement.value.offsetHeight;
          console.log(height);
        });
    
        return { myElement };
      },
    };
    </script>
    

    在 onMounted 钩子函数中,可以获取到 myElement 的高度,然后可以进行相应的操作。

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.