当前位置:网站首页>ref的使用
ref的使用
2022-08-09 16:44:00 【呦呦鹿鸣[email protected]】
一、ref 的基本使用
ref 本身是作为渲染结果被创建的
在初始渲染的时候你不能访问它们 - 它们还不存在!$refs
也不是响应式的,因此不应该试图用它在模板中做数据绑定。
ref 的基本使用,用在元素上:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<script src="js/vue.js" type="text/javascript" charset="utf-8"></script>
</head>
<body>
<div id="app">
<div ref="aa" id="aa">
我们在学习vue
</div>
<button type="button" @click="getdom">点击</button>
</div>
<script>
const vm = new Vue({
el:'#app',
data(){
return {
}
},
methods:{
getdom(){
let a = document.getElementById('aa')
console.log(a);
console.log(this.$refs.aa);
}
}
})
</script>
</body>
</html>
二、ref 在组件的使用
$refs方式:ref
被用来给元素或子组件注册引用信息。引用信息将会注册在父组件的 $refs
对象上。如果在普通的 DOM 元素上使用,引用指向的就是 DOM 元素;如果用在子组件上,引用就指向组件
ref可以调用组件中的方法:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<script src="js/vue.js" type="text/javascript" charset="utf-8"></script>
</head>
<body>
<div id="app">
<hello-world ref="hello"></hello-world>
<button @click="getHello">获取helloworld组件中的值</button>
</div>
<template id="cpn">
<div>111</div>
</template>
<script>
//子组件
const HelloWorld = {
template: '#cpn',
data() {
return {
number: 0
}
},
methods: {
handelclick() {
console.log('被调用了');
}
}
}
//父组件
const vm = new Vue({
el: '#app',
data() {
return {
}
},
components: {
HelloWorld
},
methods: {
getHello() {
console.log(this.$refs.hello);
console.log(this.$refs.hello.number);
console.log(this.$refs.hello.$el.innerHTML);
}
}
})
</script>
</body>
</html>
三、ref 的复杂使用
ref在子组件上的使用:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<script src="js/vue.js" type="text/javascript" charset="utf-8"></script>
</head>
<body>
<div id="app">
<counter @changenum="change" ref="num1"></counter>
<counter @changenum="change" ref="num2"></counter>
<h3>总价:{
{total}}</h3>
</div>
<template id="cpn">
<div @click="handelclick">{
{number}}</div>
</template>
<script>
const counter = {
template:'#cpn',
data(){
return {
number:0
}
},
methods:{
handelclick(){
this.number++
this.$emit('changenum')
}
}
}
const vm = new Vue({
el:'#app',
data(){
return {
total:0
}
},
components:{
counter
},
computed:{
/* total(){
console.log(this.$refs.num1);
return this.$refs.num1.number +this.$refs.num2.number
} */
},
methods:{
change(){
console.log(this.$refs);
this.total = this.$refs.num1.number +this.$refs.num2.number
}
}
})
//总结:ref可以实现组件通讯 同时在computed中是无法获取this.$refs的
</script>
</body>
</html>
版权声明
本文为[呦呦鹿鸣[email protected]]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_53841687/article/details/126242137
边栏推荐
猜你喜欢
基于ABP和Magicodes实现Excel导出操作
【.NET 6】开发minimal api以及依赖注入的实现和代码演示
ABP 6.0.0-rc.1的新特性
学长告诉我,大厂MySQL都是通过SSH连接的
Logic unauthorized and horizontal and vertical unauthorized payment tampering, verification code bypass, interface
MASA Stack 第三期社区例会
How to adjust futures account opening process and handling fee
消防安全培训|暑期“消防课堂”,开讲!
[ Kitex 源码解读 ] 请求重试
【.NET6+Modbus】Modbus TCP协议解析、仿真环境以及基于.NET实现基础通信
随机推荐
MySQL索引的B+树到底有多高?
方舟开服务器Vmware虚拟机安装不上?
论如何提升学习的能力
[Code Audit] - PHP project class RCE and files include download and delete
试试使用 Vitest 进行组件测试,确实很香。
C#介绍及基本数据类型
WPF效果第一百九十四篇之伸缩面板
.NET静态代码织入——肉夹馍(Rougamo) 发布1.1.0
传统数据中台又贵又复杂?何不试一试永久免费的下一代数据中台
冷冻电镜聚类中心(2D Class)粒子图像的解析
神秘的程序员(20-30)
【.NET6+Modbus】Modbus TCP协议解析、仿真环境以及基于.NET实现基础通信
EPIC是什么平台?
面试中老生常谈的MySQL问答集锦夯实基础
dotnet 6 为什么网络请求不跟随系统网络代理变化而动态切换代理
BSN季度版本2022年8月31日迭代更新预告
电子产品硬件开发中存在的问题
Tan Zhongyi: Do you know who the "Queen of Open Source" is?
体验远超Hue,这才是技术人员最喜欢的SQL工具
Apache Doris Community PMC Yang Zhengguo: How do open source projects strike a balance between their own and the community's needs?