vue3中HTML标签元素使用ref的作用

首先我们需要两个界面

APP.vue主界面

html 复制代码
<template>
  <!-- html -->
  <div class="app">
    <h1 ref="title">您好啊!</h1>
    <button @click="printTitle">点我</button> 
     <refTest/>
  </div>
</template>

<script lang="ts" setup name="App">
import {ref} from "vue";
import refTest from "./components/Ref/refTest.vue";

const title = ref<HTMLElement | null>(null);

function printTitle() {
  console.log(title.value);
}

</script>

<style scoped>
/* 样式 */
.app {
  box-shadow: 0 0 10px #000;
  background-color: rgb(88, 172, 168);
  border-radius: 10px;
  padding: 20px;
}
</style>

主界面中,定义了,ref="title",并且打印标签元素

子界面定义同名ref="title",并且打印标签元素

java 复制代码
<template>
<div class="divBox">
<h2>中国</h2>
<h2 ref="title">北京</h2>
<h2>上高速</h2>
<button @click="printTitle">打印标题标签元素</button>
</div>

</template>

<script setup  name="refTest"">
import { ref } from 'vue'

//创建一个title引用,用于存储引用
const title = ref()
function printTitle(){
    console.log(title.value);
}
</script>

<style scoped>
@import "../../Css/divBox.css";
</style>

此时我们,打印元素:

此时不受同名,ref="title"的影响,正解!!!

问题:

为什么会使用ref来获取元素标签呢?

测试:

当我们使用id来获取DOM元素的时候,id具有唯一标识,因此,当我们定义同名id时,可能就会被掩盖调。

主界面:

html 复制代码
<template>
  <!-- html -->
  <div class="app">
    <h1 id="title">您好啊!</h1>
    <button @click="printTitle">点我</button> 
     <refTest/>
  </div>
</template>

<script lang="ts" setup name="App">
import {ref} from "vue";
import refTest from "./components/Ref/refTest.vue";


function printTitle() {
  console.log(document.getElementById("title"));
}

</script>

<style scoped>
/* 样式 */
.app {
  box-shadow: 0 0 10px #000;
  background-color: rgb(88, 172, 168);
  border-radius: 10px;
  padding: 20px;
}
</style>

子界面:

html 复制代码
<template>
<div class="divBox">
<h2 id="title">中国</h2>
<h2>上高速</h2>
<button @click="printTitle">打印标题标签元素</button>
</div>

</template>

<script setup  name="refTest"">
import { ref } from 'vue'

function printTitle(){
    console.log(document.getElementById("title"));
}
</script>

<style scoped>
@import "../../Css/divBox.css";
</style>

依次点击:

由于主界面的元素先渲染的,因此打印的都是相同元素!!!

其中标签后面的,data-v-7a7a37b1是指style标签中的css样式,将scope删除,便可去掉

当我们在组件中使用ref会发生什么?

首先在子组件中添加相关数据a、b、c:

html 复制代码
<script setup  name="refTest"">
import { ref } from 'vue'

//添加多数据
const a = ref(0)
const b = ref(1)
const c = ref(2)

//创建一个title引用,用于存储引用
const title = ref()
function printTitle(){
    console.log(title.value);
}
</script>

<style scoped>
@import "../../Css/divBox.css";
</style>

父组件:

通过在组件标签上添加ref属性观察获取到数据类型:

html 复制代码
<template>
  <!-- html -->
  <div class="app">
    <h1 ref="title">您好啊!</h1>
    <button @click="printTitle">点我</button> 
     <refTest ref="testRef"/>
  </div>
</template>

<script lang="ts" setup name="App">
import {ref} from "vue";
import refTest from "./components/Ref/refTest.vue";

const title = ref<HTMLElement | null>(null);
const testRef = ref(null);
function printTitle() {
  console.log(testRef.value);
}

</script>

<style scoped>
/* 样式 */
.app {
  box-shadow: 0 0 10px #000;
  background-color: rgb(88, 172, 168);
  border-radius: 10px;
  padding: 20px;
}
</style>

浏览器打印:

并不能获取到子组件的相关数据a、b、c

因为vue在框架中对数据进行了隐藏,实现数据安全!!!

当然我们可以将数据进行手动暴露出来

在子组件中引入defineExpose

html 复制代码
<script setup  name="refTest"">

import { defineExpose } from 'vue';

e({a,b,c})

</script>

继续访问:

相关推荐
Bdygsl23 分钟前
前端开发:HTML(5)—— 表单
前端·html
望获linux28 分钟前
【实时Linux实战系列】实时数据流处理框架分析
linux·运维·前端·数据库·chrome·操作系统·wpf
red润38 分钟前
let obj = { foo: 1 };为什么Reflect.get(obj, ‘foo‘, { foo: 2 }); // 输出 1?
开发语言·javascript·ecmascript
国家不保护废物39 分钟前
TailwindCSS:原子化CSS的革命,让React开发爽到飞起!🚀
前端·css·react.js
程序视点1 小时前
如何高效率使用 Cursor ?
前端·后端·cursor
前端领航者1 小时前
重学Vue3《 v-for的key属性:性能差异与最佳实践》
前端·javascript
归于尽1 小时前
跨域问题从青铜到王者:JSONP、CORS原理详解与实战(前端必会)
前端·浏览器
Andy_GF1 小时前
纯血鸿蒙HarmonyOS Next 远程测试包分发
前端·ios·harmonyos
嗑药狂写9W行代码1 小时前
cesium修改源码支持4490坐标系
前端
小山不高2 小时前
react实现leaferjs编辑器之形状裁剪功能点
前端