标签的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>
   -->
相关推荐
阿正的梦工坊几秒前
JavaScript 闭包:从入门到精通
开发语言·javascript·ecmascript
weixin199701080161 分钟前
《闲鱼商品详情页前端性能优化实战》
前端·性能优化
qq_12084093711 分钟前
Three.js 性能实战:大场景从 15FPS 到 60FPS 的工程化优化路径
开发语言·前端·javascript
Code-keys8 分钟前
【gdb工具】 使用详细介绍
前端·chrome
guhy fighting12 分钟前
使用vue-virtual-scroller导致打包报错
前端·javascript·vue.js·webpack
UXbot14 分钟前
如何用 AI 生成产品原型:从需求描述到可交互界面的完整 5 步流程
前端·人工智能·ui·交互·ai编程
hbstream15 分钟前
Hermes Agent 一周暴涨五万 Star,但我劝你别急着追
前端·人工智能
光影少年26 分钟前
前端开发桌面端都有哪些框架?
前端·react.js·electron
Cecilialana27 分钟前
同域名、同项目、仅 hash 变化,window.location.href 不跳转
前端·javascript
Hello--_--World34 分钟前
DOM事件流与事件委托、判断数据类型、深浅拷贝、对象遍历方式
前端·javascript