vue的ref与$refs
一. ref使用在父组件上
父组件html:
<information ref='information'></information>
import information from './information'
components:{information,bill,means},
在父组件上使用子组件的值,js :this.$refs.information.isAdd; isAdd是information组件的data的属性。
二.ref使用在元素上
例如本组件html:
<span ref="myspan" class="redmy">23232</span>
本组件js使用:this.$refs["myspan"].className //redmy
this.$refs["myspan"] 指代对象//<span class="redmy">23232</span>
三.ref使用在子组件上
子组件上有
<h5 ref='insideDomRef'>我是子组件</h5>
父组件上可以引用子组件的值:this.$refs.insideDomRef// <h5 >我是子组件</h5>实例这是父组件:
<template><div>
<el-form :model="dynamicValidateForm" ref="dynamicValidateForm"
label-width="100px" class="demo-dynamic"> <el-form-item prop="email" label="邮箱" :rules="[{required: true,message:'请输入邮箱地址', trigger:'blur' },{type:'email',message:'请输入正确的邮箱地址',trigger:'blur,change'}]">
<el-input v-model="dynamicValidateForm.email" ref="myemail"></el-input>
</el-form-item> <el-form-item v-for="(domain, index) in dynamicValidateForm.domains" :label="'域名' + index"
:key="domain.key" :prop="'domains.' + index + '.value'" :rules="{required: true, message: '域名不能为空', trigger: 'blur'}">
<el-input v-model="domain.value"></el-input>
<el-button @click.prevent="removeDomain(domain)">删除</el-button>
</el-form-item> <span ref="myspan" class="redmy">23232</span> <el-form-item>
<el-button type="primary" @click="submitForm('dynamicValidateForm')">提交</el-button>
<el-button @click="addDomain">新增域名</el-button>
<el-button @click="resetForm('dynamicValidateForm')">重置</el-button>
</el-form-item> </el-form> <childone ref="childonemyyy"></childone>
</div>
</template>
<script>
import childone from './childone'
export default {
components:{childone},
data() {
return {
dynamicValidateForm: {
domains: [{
value: ''
}],
email: '',
spanval:'',
}
};
},
methods: {
submitForm(formName) { this.$refs["childonemyyy"].isAdd;//"mychildone"用在父组件上引用子组件值,返回子组件上的data数据
this.$refs["myspan"].className //redmy 用在元素上,返回元素节点对象 this.$refs[formName].validate((valid) => { if (valid) {
alert('submit!'+this.$refs[formName].email);
} else {
console.log('error submit!!');
return false;
}
});
},
resetForm(formName) {
this.$refs[formName].resetFields();
},
removeDomain(item) {
var index = this.dynamicValidateForm.domains.indexOf(item)
if (index !== -1) {
this.dynamicValidateForm.domains.splice(index, 1)
}
},
addDomain() {
this.dynamicValidateForm.domains.push({
value: '',
key: Date.now()
});
}
}
}
</script>
这是子组件:
<template><div>
<p class="ppp">我是p段落,我是子组件一</p>
<el-button @click="submit">子组件</el-button>
</div>
</template>
<script type="text/javascript">
export default {
data(){
return{
isAdd:"mychildone",
}
},
methods:{
submit(){
} }, created(){ } } </script>
jieshu
vue的ref与$refs的更多相关文章
- vue.js中ref及$refs的使用及讲解
关于ref和$refs的用法及讲解,vue.js中文社区( https://cn.vuejs.org/v2/api/#ref )是这么讲解的: ref 被用来给元素或子组件注册引用信息,引用信息将会注 ...
- Vue.js-11:第十一章 - Vue 中 ref 的使用
一.前言 在之前的前端开发中,为了实现我们的需求,通常采用的方案是通过 JS/Jquery 直接操纵页面的 DOM 元素,得益于 Jquery 对于 DOM 元素优异的操作能力,我们可以很轻易的对获取 ...
- vue之ref
ref 被用来给元素或子组件注册引用信息.引用信息将会注册在父组件的 $refs 对象上.如果在普通的 DOM 元素上使用,引用指向的就是 DOM 元素:如果用在子组件上,引用就指向组件. 1.ref ...
- Vue使用Ref跨层级获取组件实例
目录 Vue使用Ref跨层级获取组件实例 示例介绍 文档目录结构 安装vue-ref 根组件自定义方法[使用provide和inject] 分别说明各个页面 结果 Vue使用Ref跨层级获取组件实例 ...
- vue里ref ($refs)用法
ref 有三种用法: 1.ref 加在普通的元素上,用this.ref.name 获取到的是dom元素 2.ref 加在子组件上,用this.ref.name 获取到的是组件实例,可以使用组件的所有方 ...
- vue中的 ref 和 $refs
如图,ref 被用来给元素或子组件注册引用信息.引用信息将会注册在父组件的 $refs 对象上.如果在普通的 DOM 元素上使用,引用指向的就是 DOM 元素:如果用在子组件上,引用就指向组件实例: ...
- vue中ref的使用(this.$refs获取为undefined)
如果你获取到的总是空的,你注意一下: 1.你在哪里调用,和你调用的对象 试试在mounted()里面调用有效果没有 调用的对象是本来就存在的,还是需要数据渲染之后才会出现的,同理,在mounted() ...
- Vue 获取dom元素之 ref 和 $refs 详解
一.$refs 一个对象,持有ref注册过的所有元素或子组件.(注册过的 ref 的集合) 二.ref 被用来给元素或子组件注册引用信息.若用在dom元素上,引用指向的就是dom元素:若用在子组件上, ...
- 理解Vue中的ref和$refs
参考博客:https://www.cnblogs.com/xumqfaith/p/7743387.html
随机推荐
- 利用flume+kafka+storm+mysql构建大数据实时系统
架构图
- 【转】Native Thread for Win32 A- Create Thread(通俗易懂,非常好)
http://www.bogotobogo.com/cplusplus/multithreading_win32A.php Microsoft Windows operating system's s ...
- Angular ViewChild
viewchild // 使用方法 git clone https://git.oschina.net/mumu-osc/learn-component.git cd learn-component ...
- input的disable和readonly
在设计网页时,有时需要将输入框设置为只读状态,即其中的内容不可编辑,实现这种设计的方法有两种:使用input的disable和readonly两个属性. 先来看下二者的区别: <input ty ...
- 160425、linux安装SVN服务器
1:查看linux是否已经安装svn服务 [root@nb ~]# rpm -qa subversion subversion-1.6.11-15.el6_7.x86_64 2:安装svn #yum ...
- Struts2中解决表单重复提交
3. 表单的重复提交问题 1). 什么是表单的重复提交 > 在不刷新表单页面的前提下: >> 多次点击提交按钮 >> 已经提交成功, 按 "回退" ...
- ERR_PTR,PTR_ERR还有IS_ERR函数详解
内核中的函数常常返回指针,问题是如果出错,也希望能够通过返回的指针体现出来. 总体来说,如果内核返回一个指针,那么有三种情况:合法指针,NULL指针和非法指针. 1)合法指针:内核返回的指针一般是指向 ...
- Results from queries can be retrieved into local variables
w将查询结果赋值给本地变量. http://dev.mysql.com/doc/refman/5.7/en/stored-program-variables.html Results from que ...
- Apache Kafka源码分析 – Controller
https://cwiki.apache.org/confluence/display/KAFKA/Kafka+Controller+Internalshttps://cwiki.apache.org ...
- c++ caffe 输出 activation map 、 层参数
python输出activation map与层参数:https://blog.csdn.net/tina_ttl/article/details/51033660 caffe::Net文档: htt ...