标签的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>
   -->
相关推荐
大怪v33 分钟前
AI抢饭?前端佬:我要验牌!
前端·人工智能·程序员
新酱爱学习33 分钟前
字节外包一年,我的技术成长之路
前端·程序员·年终总结
小兵张健42 分钟前
开源 playwright-pool 会话池来了
前端·javascript·github
IT_陈寒4 小时前
Python开发者必知的5大性能陷阱:90%的人都踩过的坑!
前端·人工智能·后端
codingWhat4 小时前
介绍一个手势识别库——AlloyFinger
前端·javascript·vue.js
Lee川4 小时前
深度拆解:基于面向对象思维的“就地编辑”组件全模块解析
javascript·架构
代码老中医4 小时前
2026年CSS彻底疯了:这6个新特性让我删掉了三分之一JS代码
前端
进击的尘埃4 小时前
Web Worker 与 OffscreenCanvas:把主线程从重活里解放出来
javascript
不会敲代码14 小时前
Zustand:轻量级状态管理,从入门到实践
前端·typescript
踩着两条虫4 小时前
VTJ.PRO 双向代码转换原理揭秘
前端·vue.js·人工智能