vue基本使用--refs获取组件或元素的实例
作者:晨曦之光11 发布时间:2024-05-02 16:35:20
标签:vue,refs,组件,元素
说明:vm.$refs 一个对象,持有已注册过 ref 的所有子组件(或HTML元素)
使用:在 HTML元素 中,添加ref属性,然后在JS中通过vm.$refs.属性来获取
注意:如果获取的是一个子组件,那么通过ref就能获取到子组件中的data和methods
添加ref属性
<div id="app">
<h1 ref="h1Ele">这是H1</h1>
<hello ref="ho"></hello>
<button @click="getref">获取H1元素</button>
</div>
获取注册过 ref 的所有组件或元素
methods: {
getref() {
// 表示从 $refs对象 中, 获取 ref 属性值为: h1ele DOM元素或组件
console.log(this.$refs.h1Ele.innerText);
this.$refs.h1ele.style.color = 'red';// 修改html样式
console.log(this.$refs.ho.msg);// 获取组件数据
console.log(this.$refs.ho.test);// 获取组件的方法
}
}
来源:https://blog.csdn.net/qq_21796899/article/details/78752703
0
投稿
猜你喜欢
- 这题的官方难度是Medium,点赞1296,反对505,通过率35.4%。从各项指标来说看起来有些中规中矩,实际上也的确如此。这道题的解法和
- 这里想象一下需求,写一个项目使用的一系列1.0版本的插件,现在要新写一个项目,需要用这些插件的2.0版本,该怎么办?都更新成2.0版本?这样
- 本文实例讲述了Python实现二分查找算法的方法。分享给大家供大家参考。具体实现方法如下:#!/usr/bin/env pythonimpo
- 本文实例讲述了python获得两个数组交集、并集、差集的房部分。分享给大家供大家参考。具体如下:1. 获取两个list 的交集#方法一:a=
- Python 的元组与列表类似,不同之处在于元组的元素不能修改。元组使用小括号,列表使用方括号。元组创建很简单,只需要在括号中添加元素,并使
- 本文实例为大家分享了JS实现轮播图特效的具体代码,供大家参考,具体内容如下知识点轮播图思想:① 建立一个全局变量索引,始终标记当前显示图片。
- 1. AdaBoost 算法简介Boosting是机器学习的三大框架之一,其特点是,训练过程中的诸多弱模型,彼此之间有着强依赖关系。Boos
- TCP是用来计算机之间进行通信的,通过编写客户端和服务端聊天的代码,对于服务器与客户端的工作步骤有了深刻的了解,在这里根据了不起的Node.
- 本文实例讲述了Python3.4列表、数组操作。分享给大家供大家参考,具体如下:python列表,数组类型要相同,python不需要指定数据
- 目的现有两幅栅格图像,一个是某地区道路栅格图,一个是某地区土地利用类型图,需要将道路叠加到土地利用类型图中,即叠加后,重合的像元值以道路图为
- Elasticsearch查询查询分类:基本查询:使用es内置查询条件进行查询组合查询:把多个查询组合在一起进行复合查询过滤:查询的同时,通
- Analyze Table MySQL 的Optimizer(优化元件)在优化SQL语句时,首先需要收集一些相关信息,其中就包括表的card
- 何为自省在计算机编程领域里,自省是一种能力,是通过一定机制在程序运行时获知对象的类型及对象的内部结构,Python的自省能力还是很强大的,因
- Python的运算符和其他语言类似(我们暂时只了解这些运算符的基本用法,方便我们展开后面的内容,高级应用暂时不介绍)数学运算>>
- 我们已经知道Application和Session对象的OnStart、OnEnd事件的脚本,都必须是在 Global.asa&n
- 如:http://localhost/newurl/WebForm1.aspx 1.方法document.URL(注意大小写) 结果是:ht
- 上一篇内容,已经学会了使用简单的语句对网页进行抓取。接下来,详细看下urlopen的两个重要参数url和data,学习如何发送数据data一
- 在我们学习python的过程中,学习序列是一门必修课。当我们掌握了序列过后,便会学习常用的两个排序函数sort()与sorted()。但很少
- 一、 网页设计中的对比原则一件网页设计作品由许多元素构成,它们的重要性各不相同,其中一些元素的重要性要高于其它元素。一些相互关联,而另一些则
- 前言:在转换操作中,我们执行各种操作,例如更改系列的数据类型,将系列更改为列表等。为了执行转换操作,我们有各种有助于转换的功能,例如.ast