vue3 ref获取元素

2023年1月28日12:26:34

ref获取元素

  • 当setup写在script中的时候
    <template>
      <div ref="dom"></div>
    </template>
    
    <script setup>
    import { ref, onMounted } from "vue";
    const dom = ref(null);
    onMounted(() => {
      console.log(dom.value);
    });
    </script>
    
  • 当setup是单独写的时候
    <template>
      <div ref="dom"></div>
    </template>
    
    <script>
    import { ref, onMounted } from "vue";
    export default {
    	setup() {
    		const dom = ref(null);
    		onMounted(() => {
    			 console.log(dom.value);
    		});
    		return { dom }
    	}
    }
    </script>
    
  • 作者:Br不二
  • 原文链接:https://blog.csdn.net/weixin_39519297/article/details/128001971
    更新时间:2023年1月28日12:26:34 ,共 376 字。