首页 > 其他分享 >useTemplateRef使用

useTemplateRef使用

时间:2024-09-26 09:13:46浏览次数:1  
标签:const useTemplateRef value 使用 inputEl inputEl1 ref

1、使用ref方式:

<template>
  <div>
    <input type="text" ref="inputEl" />
    <button @click="setInputValue">给input赋值</button>
  </div>
</template>

<script lang="ts" setup>
import { ref } from "vue";

const inputEl = ref<HTMLInputElement>();

function setInputValue() {
  if (inputEl.value) {
    inputEl.value.value = "Hello, world!";
  }
}

</script>

2、使用useTemplateRef方式

<template>
  <input type="text" ref="inputRef" />
  <button @click="setInputValue">给input赋值</button>
</template>

<script setup lang="ts">
import { useTemplateRef } from "vue";

const inputEl = useTemplateRef<HTMLInputElement>("inputRef");
  
function setInputValue() {
  if (inputEl.value) {
    inputEl.value.value = "Hello, world!";
  }
}
</script>

3、使用useTemplateRef的应用

<template>
  <input type="text" ref="inputEl1" />
  <input type="text" ref="inputEl2" />
  <button @click="switchRef">切换ref绑定的变量</button>
  <button @click="setInputValue">给input赋值</button>
</template>

<script setup lang="ts">
import { useTemplateRef, ref } from "vue";

const refKey = ref("inputEl1");
const inputEl1 = useTemplateRef<HTMLInputElement>("inputEl1");
const inputEl2 = useTemplateRef<HTMLInputElement>("inputEl2");
  
function switchRef() {
  refKey.value = refKey.value === "inputEl1" ? "inputEl2" : "inputEl1";
}
function setInputValue() {
  const curEl = refKey.value === "inputEl1" ? inputEl1 : inputEl2;
  if (curEl.value) {
    curEl.value.value = "Hello, world!";
  }
}
</script>

标签:const,useTemplateRef,value,使用,inputEl,inputEl1,ref
From: https://www.cnblogs.com/springsnow/p/18432714

相关文章

  • PC机上使用qemu-user-static + Docker实现arm/aarch64本地编译
    参考的链接:https://www.cnblogs.com/chen2ha/p/17180287.html文章很长,一些关键点在:取得docker,Docker可以通过Linux发布版中取得,也可以从docker网站上下载编译好的程序。下载链接:https://download.docker.com/linux/static/stable/x86_64/这里下载的是PC上的Docker,如果需要a......
  • vue3中使用百度地图
    1、在入口的index.html中添加以下代码,更换成自己的key<scriptsrc="//api.map.baidu.com/api?type=webgl&v=1.0&ak=yourkey"></script>2、新增一个.d.ts文件,全局声明BMapGL变量declareconstBMapGL:any不然的话会报这种错误找不到名称“BMapGL”。3、增加地图......
  • ELK中日志数据采集器Filebeat的安装和使用、Filebeat结合Logstash进行日志处理入Elast
    一、ELK中日志数据采集器Filebeat的安装和使用    Beats是数据采集的得力工具,Beats能够将数据转发至Logstash进行转换和解析。Filebeat是Beats中的一种,Filebeat是本地文件的日志数据采集器,可监控日志目录或特定日志文件(tailfile),并将它们转发给Elasticsearch或Logstats......
  • CentOS8使用chrony同步网络时间
    文章目录引言ICentOS8使用chrony网络时间同步安装chrony配置间同步服务器地址检查本机的时区设置时区chronyc命令IIwindows网络时间同步2.1修改同步服务器2.2修改同步频率引言应用场景:获取服务器时间进行船舶在线率统计dtos.forEa......
  • 使用nc命令检测UDP端口
    使用nc命令检测UDP端口也是非常的简单,需要注意的是,所安装nc的版本不同,使用选项有点差异。1、检测开启的UDPnc-vuz192.168.2.2015353nc-vuz192.168.2.20137430端口正常启用时,会提示“UDPpacketsentsuccessfully”2、检测未开启的UDPnc-vuz192.168.2.2015354n......
  • 使用dockerfile来构建一个包含Jdk17的centos7镜像
    文章目录1、dockerfile简介2、入门案例2.1、创建目录/opt/dockerfilejdk172.2、上传jdk-17_linux-x64_bin.tar.gz到/opt/dockerfilejdk172.3、在/opt/dockerfilejdk17目录下创建dockerfile文件2.4、执行命令构建镜像:不要忘了后面的那个.2.5、查看镜像是否建立完成2......
  • BeautifulSoup4在爬虫中的使用
    一、BeautifulSoup4简介BeautifulSoup提供一些简单的python函数来处理导航、搜索等功能。它是一个工具箱,是python的一个库,最主要的功能是从网页获取数据。二、BeautifulSoup4安装在cmd下安装pipinstallbeautifulsoup4三、BeautifulSoup4支持的主要解析器:1、Pytho......
  • 使用AI进行需求分析的案例研究
    生成式AI的潜在应用场景似乎无穷无尽。虽然这令人兴奋,但也可能让人不知所措。因此,团队在使用这项技术时需要有明确的目标:关键是要明确生成式AI在团队工作中能产生哪些实质性影响。在软件工程中,一个引人注目的应用场景是需求分析。这是一个常常被忽视但充满挑战的环节,如果处理......
  • getopt函数的使用
    getopt函数intgetopt(intargc,char*constargv[],constchar*optstring);头文件:#include<unistd.h>参数:argc:命令行参数的数量**argv*8:一个字符串数组,包含所有命令行参数**optstring*8:一个字符串,定义了合法的选项字符(例如:"abc:"表示-a,-b,和-c需......
  • 使用 Vue3、TypeScript 和 Spring Boot 实现文件上传至 MinIO 和 OSS
    目录《使用Vue3、TypeScript和SpringBoot实现文件上传至MinIO和OSS》一、技术选型二、环境搭建三、前端实现四、后端实现五、代码解析在现代web应用开发中,文件上传是一个常见的需求。本文将介绍如何使用Vue3、TypeScript和SpringBoot实现文件上传功能,并......