标签的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>
   -->
相关推荐
镜宇秋霖丶8 小时前
2026.5.6@霖宇博客制作中遇见的问题
前端·javascript·vue.js
吴声子夜歌8 小时前
Vue3——TypeScript基础
javascript·typescript
小李子呢02119 小时前
前端八股Vue---Vue-router路由管理器
前端·javascript·vue.js
百锦再10 小时前
Auto.js变成基础知识学习
开发语言·javascript·学习·sqlite·kotlin·android studio·数据库开发
洛_尘10 小时前
Python 5:使用库
java·前端·python
Bigger10 小时前
Bun 能上生产吗?我的实战结论
前端·node.js·bun
kyriewen12 小时前
你的前端滤镜慢得像PPT?用Rust+WebAssembly,一秒处理4K图
前端·rust·webassembly
kyriewen1112 小时前
你等的Babel编译,够喝三杯咖啡了——用Rust重写的SWC,只需眨个眼
开发语言·前端·javascript·后端·性能优化·rust·前端框架
IT_陈寒12 小时前
SpringBoot自动配置坑了我,原来要这样绕过去
前端·人工智能·后端
东方小月12 小时前
Claude Code 完整上手指南:MCP、Skills、第三方模型配置一次搞定
前端·人工智能·后端