标签的ref属性

如果想获取一个dom元素,但是两个组件都有相同的id就会优先加载第一个的,ref就是为了解决这个问题的

html 复制代码
​<template>
      <div class="person">
         <div id="title">你好</div>
         <button @click="handleClick">点击我</button>
      </div>
</template>
<script setup>
   const handleClick = () => {
      console.log(document.getElementById('title'))
   }
</script>
<style lang="scss" scoped>
            .person {
               background-color: skyblue;
               box-shadow: 0 0 10px;
               border-radius: 10px;
               padding: 20px;
            }
            button {
               margin-right: 10px;
            }
</style>

​
html 复制代码
<template>
      <div id="title">app</div>
      <Person/>

</template>

<script setup name="App"> 
   import Person from './componets/person.vue'
   // export default {
   //    name:'App',//组件名
   //    components:{Person}//注册组件
   // }
</script>
<!-- <style>
 .app {
      background-color: #ddd;
      box-shadow: 0 0 10px;
      border-radius: 10px;
      padding: 20px;
   }
</style>
   -->

ref加在html标签上

html 复制代码
​<template>
      <div class="person">
         <div ref="title">你好</div>
         <button @click="handleClick">点击我</button>
      </div>
</template>
<script setup>
   import{ref} from 'vue'
   const title=ref()
   const handleClick = () => {
      console.log(title.value);
   }
</script>
<style lang="scss" scoped>
            .person {
               background-color: skyblue;
               box-shadow: 0 0 10px;
               border-radius: 10px;
               padding: 20px;
            }
            button {
               margin-right: 10px;
            }
</style>

​
html 复制代码
<template>
      <div ref="title">app</div>
      <button @click="handleClick">点击我</button>
      <Person/>
</template>

<script lang="ts"  setup name="App"> 
   import Person from './componets/Person.vue'
   import {ref} from 'vue'
   const title=ref()
   const handleClick = () => {
      console.log(title.value);
   }

   // export default {
   //    name:'App',//组件名
   //    components:{Person}//注册组件
   // }
</script>
<!-- <style>
 .app {
      background-color: #ddd;
      box-shadow: 0 0 10px;
      border-radius: 10px;
      padding: 20px;
   }
</style>
   -->

ref加在组件标签上

出现组件实例

html 复制代码
<template>
      <div ref="title">app</div>
      <button @click="handleClick">点击我</button>
      <Person ref="ren"/>
</template>

<script lang="ts"  setup name="App"> 
   import Person from './componets/Person.vue'
   import {ref} from 'vue'
   const title=ref()
   const handleClick = () => {
      console.log(title.value);
   }
   const ren=ref()
   console.log(ren);
   // export default {
   //    name:'App',//组件名
   //    components:{Person}//注册组件
   // }
</script>
<!-- <style>
 .app {
      background-color: #ddd;
      box-shadow: 0 0 10px;
      border-radius: 10px;
      padding: 20px;
   }
</style>
   -->
相关推荐
yuanyxh2 小时前
macOS 应用 - 纯对话生成
前端·macos·ai编程
大家的林语冰2 小时前
ES5 凉凉,Babel 8 正式发布,默认不再编译为 ES5 和 CJS......
前端·javascript·前端工程化
光影少年3 小时前
react批量更新、同步/异步更新场景
前端·react.js·掘金·金石计划
假如让我当三天老蒯3 小时前
模块化:ES Module 与 CommonJS 的区别
前端·面试
用户40950115773173 小时前
Private Forge v2.0 发布:12大前端业务场景技能系统
前端
weedsfly4 小时前
异步编程全景与事件循环——彻底搞懂 JS 执行机制
前端·javascript
用户059540174464 小时前
AI Agent记忆测试踩坑实录:Mock骗了我一周,Mem0+pytest一招破局
前端·css
用户1733598075374 小时前
纯前端 PDF 数字签名实战:Vue 3 + pdf-lib 在浏览器里完成签名嵌入
前端·javascript
IT_陈寒5 小时前
SpringBoot自动配置的坑,我爬了三天才出来
前端·人工智能·后端
Avan_菜菜12 小时前
AI 能写代码了,为什么我反而开始要求它先写文档?
前端·github·ai编程