16 Vue3中的refs引用

概述

In Vue, refs are references to DOM elements or other component instances that have been mounted to the DOM.

在 Vue 中,Refs 是对 DOM 元素或已安装到 DOM 的其他组件实例的引用。

One of the major use cases for refs is direct DOM manipulation and integration with DOM-based libraries (that usually take a DOM node they should mount to), such as an animation library.

refs 的主要用途之一是直接操作 DOM 并与基于 DOM 的库集成(这些库通常需要挂载到一个 DOM 节点),例如动画库。

We define refs by using the syntax ref=“name” on a native element or child component in the template. In the following example, we will add a reference to the input element under the name theInput:

我们可以在模板中的原生元素或子组件上使用 ref=“name” 语法来定义引用。在下面的示例中,我们将为输入元素添加一个引用,名称为 theInput:

<template><div id="app"><input ref="theInput" /></div>
</template>

Refs can be accessed from the Vue component instance through this. r e f s [ r e f N a m e ] . S o , i n t h e p r e c e d i n g e x a m p l e , w h e r e w e h a d a r e f d e f i n e d a s r e f = " t h e I n p u t " , i t c a n b e a c c e s s e d t h r o u g h t h i s . refs[refName]. So, in the preceding example, where we had a ref defined as ref="theInput", it can be accessed through this. refs[refName].So,intheprecedingexample,wherewehadarefdefinedasref="theInput",itcanbeaccessedthroughthis.refs.theInput.

可以通过 this. r e f s [ r e f N a m e ] 访问 V u e 组件实例中的 r e f 。因此,在前面的示例中,我们将 r e f 定义为 r e f = " t h e I n p u t " ,可以通过 t h i s . refs[refName] 访问 Vue 组件实例中的 ref。因此,在前面的示例中,我们将 ref 定义为 ref="theInput",可以通过 this. refs[refName]访问Vue组件实例中的ref。因此,在前面的示例中,我们将ref定义为ref="theInput",可以通过this.refs.theInput 进行访问。

Now let’s programmatically focus on the input field when clicking the Focus Input button, as follows:

现在,让我们通过编程,在单击 "聚焦输入 "按钮时聚焦输入框,如下所示:

<template><div id="app"><input ref="theInput" /><button @click="focus()">Focus Input</button></div>
</template>
<script>
export default {methods: {focus() {this.$refs.theInput.focus()}}
}
</script>

Note here that we can only access r e f s o n c e t h e c o m p o n e n t i s m o u n t e d t o t h e D O M . H e n c e t h i s . refs once the component is mounted to the DOM. Hence this. refsoncethecomponentismountedtotheDOM.Hencethis.refs.theInput in our example is only available in the mounted() life cycle hook. Also, if you use <script setup>, there is no $refs available since there is no this and setup runs before the component instance is created. Hence to use DOM references with <script setup> or the setup hook, we use the ref() function from the Composition API instead.

请注意,只有当组件挂载到 DOM 时,我们才能访问 r e f s 。因此,我们示例中的 t h i s . refs。因此,我们示例中的 this. refs。因此,我们示例中的this.refs.theInput 只能在 mounted() 生命周期钩子中使用。此外,如果使用 <script setup>,由于在创建组件实例之前没有 this 和 setup,因此没有 $refs 可用。因此,要在 <script setup> 或设置钩子中使用 DOM 引用,我们可以使用 Composition API 中的 ref() 函数。

We have learned how to use $refs to access the DOM elements from the component. When you need select a DOM node directly, we recommend you use a ref instead of using the DOM API (querySelector/querySelectorAll).

我们已经学会了如何使用 $refs 访问组件中的 DOM 元素。当您需要直接选择 DOM 节点时,我们建议您使用 ref 而不是使用 DOM API(querySelector/querySelectorAll)。

In the following exercise, we will learn how the Countable library helps increase interactivity in a project.

在下面的练习中,我们将学习 Countable 库如何帮助提高项目的交互性。

练习:使用Countable.js库

Countable is a library that, given an element (usually an HTML textarea or input), will add live counts of paragraphs, words, and characters. Live metrics on the text being captured can be quite useful to increase interactivity in a project where editing text is a core concern.

Countable 是一个库,当给定一个元素(通常是 HTML 文本区域或输入)时,它将添加段落、单词和字符的实时计数。在以编辑文本为核心关注点的项目中,对所捕获文本的实时度量对于提高交互性非常有用。

Create a new src/components/TextEditorWithCount.vue component with a textarea that we will have a ref to:

创建一个新的 src/components/TextEditorWithCount.vue 组件,其中包含一个文本区,我们将为其创建一个引用:

<template>
<div><textarearef="textArea"cols="50"rows="7"></textarea>
</div>
</template>

Next, we will import and render the component in src/App.vue:

接下来,我们将在 src/App.vue 中导入并渲染该组件:

<template><div id="app"><TextEditorWithCount/></div>
</template>
<script>
import TextEditorWithCount from './components/TextEditorWithCount.vue'export default {components: {TextEditorWithCount}
}
</script>

线安装Countable:

yarn add countable# 或者
npm add countable

We now need to integrate Countable. We will import it and initialize it with this.$refs.textArea. We will also store the counts on the instance as this.count:

现在我们需要集成 Countable。我们将导入 Countable 并用 this.$refs.textArea 进行初始化。我们还将以 this.count 的形式在实例中存储计数:

<script>
import * as Countable from 'countable'export default {mounted() {Countable.on(this.$refs.textArea, (count) => {this.count = count})},data() {return {count: null}}
}
</script>

With a small update to template, we can display the counts we care about:

只需对模板稍作更新,我们就能显示我们关心的计数:

<template><div><!--段落--><textarearef="textArea"cols="50"rows="7"></textarea><!--段落计数--><ul v-if="count"><li>Paragraphs: {{ count.paragraphs }}</li><li>Sentences: {{ count.sentences }}</li><li>Words: {{ count.words }}</li></ul></div>
</template>

One last thing we need to do is remove the Countable event listener when the component is unmounted:

我们需要做的最后一件事是在组件卸载时移除 Countable 事件监听器:

<script>
import * as Countable from 'countable'export default {/*** 挂载方法,监听文本域*/mounted() {Countable.on(this.$refs.textArea, (count) => {this.count = count})},/*** 卸载之前,移除监听*/beforeUnmount() {Countable.off(this.$refs.textArea)},data() {return {count: null}}
}
</script>

This integration of a JavaScript/DOM library inside a Vue app is a key application of Vue refs. Refs allow us to pick from the existing ecosystem of libraries and wrap or integrate them into a component.

在 Vue 应用程序中集成 JavaScript/DOM 库是 Vue Refs 的一项重要应用。通过 Refs,我们可以从现有的库生态系统中挑选库,并将它们封装或集成到一个组件中。

Vue refs are useful for integrating DOM libraries or for accessing DOM APIs directly.

Vue refs 可用于集成 DOM 库或直接访问 DOM API。

To round off our examination of component composition, we need to know how to pass data from child components back to their parents, which we will explore next.

为了完成对组件构成的研究,我们需要知道如何将子组件的数据传回父组件,接下来我们将探讨这一点。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/229512.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【具身智能评估3】具身视觉语言规划(EVLP)度量标准汇总

参考论文&#xff1a;Core Challenges in Embodied Vision-Language Planning 论文作者&#xff1a;Jonathan Francis, Nariaki Kitamura, Felix Labelle, Xiaopeng Lu, Ingrid Navarro, Jean Oh 论文原文&#xff1a;https://arxiv.org/abs/2106.13948 论文出处&#xff1a;Jo…

netty-daxin-4(httpwebsocket)

文章目录 学习链接http服务端NettyHttpServerHelloWorldServerHandler 客户端ApiPost websocket初步了解为什么需要 WebSocket简介 浏览器的WebSocket客户端客户端的简单示例客户端的 APIWebSocket 构造函数webSocket.readyStatewebSocket.onopenwebSocket.onclosewebSocket.ο…

Unity 使用AddRelativeForce方法给刚体施加力详解

之前我们总结过使用AddForce方法给刚体施加力&#xff1a;Unity 使用AddForce方法给刚体施加力详解-CSDN博客 本文总结另外一个方法AddRelativeForce&#xff0c;本质上它们区别不大&#xff0c;都是给刚体施加一个持续的力&#xff0c;唯一区别就是施加力时候使用的坐标系不一…

MATLAB - MPC - QP Solvers

系列文章目录 前言 模型预测控制器 QP 求解器将线性 MPC 优化问题转换为一般形式的 QP 问题 受到线性不等式约束 其中 x 是解向量。H 是黑森矩阵。当预测模型和调整权重在运行时不发生变化时&#xff0c;该矩阵保持不变。A 是线性约束系数矩阵。当预测模型在运行时不发生变化时…

Eclipse 自动生成注解,如果是IDEA可以参考编译器自带模版进行修改

IDEA添加自动注解 左上角选择 File -> Settings -> Editor -> File and Code Templates&#xff1b; 1、添加class文件自动注解&#xff1a; ​/*** <b>Function: </b> todo* program: ${NAME}* Package: ${PACKAGE_NAME}* author: Jerry* date: ${YEA…

介绍strncpy函数

strncpy函数需要引用#include <string.h>头文件 函数原型&#xff1a; char *_Dest 是字符串的去向 char *_Source是字符串的来源 size_t_Count是复制字符串的大小 #include <stdio.h> #include <string.h> int main() { char arr[128] { \0 }; …

【JAVA-Day69】抛出异常的精髓:深度解析 throw、throws 关键字,优雅处理异常问题

抛出异常的精髓&#xff1a;深度解析 throw、throws 关键字&#xff0c;优雅处理异常问题 &#x1f680; 抛出异常的精髓&#xff1a;深度解析 throw、throws 关键字&#xff0c;优雅处理异常问题 &#x1f680;一、什么是抛出异常 &#x1f60a;二、如何抛出异常 &#x1f914…

什么是计算机网络?计算机网络基础知识

1.网络的组成部分&#xff1a;由主机&#xff0c;路由器&#xff0c;交换机等组成 2.网络结构&#xff1a;网络的网络 3.信息交换方式&#xff1a;电路交换和分组交换 4.网络分层&#xff1a;分清职责&#xff0c;物理层&#xff0c;链路层&#xff0c;网络层&#xff0c;运…

IDEA中如何使用Vue

一、在IDEA中安装Vue插件 1.先点击File-->Settings-->Plugins 2.点击进去后在Marketplace下的搜索框搜索Vue.js插件进行下载&#xff0c;下载完后点击Install 3.上一步操作进行完后&#xff0c;点击Installed在其搜索框下搜索Vue插件并且进行勾选&#xff0c;勾选后点…

0x23 剪枝

0x23 剪枝 剪枝&#xff0c;就是减少搜索树规模&#xff0c;尽早排除搜索树中不必要的分支的一种手段。形象地看&#xff0c;就好像剪掉了搜索树的枝条&#xff0c;故被称为“剪枝”。在深度优先搜索中&#xff0c;有以下几类常见的剪枝方法&#xff1a; 1.优化搜索顺序 在一…

MetaAI语音翻译大模型Seamless登场,主打AI无缝同声传译

论文题目&#xff1a; Seamless: Multilingual Expressive and Streaming Speech Translation 论文链接&#xff1a; https://ai.meta.com/research/publications/seamless-multilingual-expressive-and-streaming-speech-translation/ 代码链接&#xff1a; GitHub - facebook…

DSP捕获输入简单笔记

之前使用stm32的大概原理是&#xff1a; 输入引脚输入一个脉冲&#xff0c;捕获1开始极性捕获&#xff0c;捕获的是从启动捕获功能开始计数&#xff0c;捕获的是当前的计数值&#xff1b; 例如一个脉冲&#xff0c;捕获1捕获上升沿&#xff0c;捕获2捕获下降沿&#xff1b;而两…

爬虫工作量由小到大的思维转变---<第十二章 Scrapy之sql存储与爬虫高效性的平衡艺术>

前言: (本文仅属于技术性探讨,不属于教文) 刚好&#xff0c;前阵子团队还在闲聊这个问题呢。你知道吗&#xff0c;在数据收集这个行当里&#xff0c;怎么存数据这问题就跟“先有鸡还是先有蓝”一样&#xff0c;没完没了的循环往复。老规矩&#xff0c;咱们先搞清楚我们的“鸡…

reactive数据不响应

我们知道&#xff0c;reactive函数用于创建对象等复杂数据的响应式代理对象&#xff0c;当该对象的属性发生变化时&#xff0c;会自动触发视图更新。 但在Vue 3中&#xff0c;当我们使用reactive创建的对象或数组进行赋值时&#xff0c;尽管能够完成正常的赋值操作&#xff0c…

Linux 系统开机启动流程

可能没有完全理解&#xff0c;后期整理完Linux的内容&#xff0c;应该理解会深入一些&#xff0c;试着用更简洁的方式和图形来记录&#xff0c;以及一些概念的完善 2023-12-14 一、开机流程 BIOS MBR/GPT 加载 BIOS 的硬件信息与进行自检&#xff0c;并依据设定取得第一个可…

TrustZone之调试、跟踪和分析

接下来,我们将查看系统中的调试和跟踪组件,如下图所示: 现代Arm系统包括支持调试和性能分析的广泛功能。在TrustZone中,我们必须确保这些功能不能被用来危害系统的安全性。 关于调试功能,考虑开发新的SoC。不同的开发人员被信任调试系统的不同部分。芯片公司的工程…

第十九章 : Spring Boot 集成RabbitMQ(三)

第十九章 : Spring Boot 集成RabbitMQ(三) 前言 本章节重点:RabbitMQ消息确认机制的代码示例:生产者消息确认机制、Return消息机制、消费端ACK和Nack机制3种消息确认模式。 Springboot 版本 2.3.2.RELEASE ,RabbitMQ 3.9.11,Erlang 24.2消息确认的场景 使用RabbitMQ很…

SearchWP WordPress高级网站内容搜索插件(包含所有专业扩展)

点击阅读SearchWP WordPress高级网站内容搜索插件(包含所有专业扩展)原文 SearchWP WordPress高级网站内容搜索插件是一个非常强大的工具&#xff0c;可以显着增强您网站的搜索功能。通过向网站访问者提供高度相关和精确的搜索结果&#xff0c;它可以有效地简化他们的搜索过程…

C语言使用posix正则表达式库

在C语言中&#xff0c;你可以使用 POSIX 正则表达式库&#xff08;regex.h&#xff09;来进行正则表达式的模式匹配。POSIX 正则表达式库提供了一组函数来编译、执行和释放正则表达式。 下面是使用 POSIX 正则表达式库的基本步骤&#xff1a; 包含头文件 <regex.h>&…

项目管理软件助力科研项目管理

作为一名研究人员&#xff0c;你可能会觉得你的成功取决于你的研究有多创新和你工作有多努力。实际上&#xff0c;创新和勤奋很重要&#xff0c;但聪明地工作也很重要。如果你是那种在早上打开电子邮件并开始自上而下的工作的人&#xff0c;你可能很快就会发现你的电子邮件多得…