一. 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>

vue ref父子组件传值的更多相关文章

  1. vue 非父子组件传值

    /*非父子组件传值 1.新建一个js文件 然后引入vue 实例化vue 最后暴露这个实例 2.在要广播的地方引入刚才定义的实例 3.通过 VueEmit.$emit('名称','数据') 4.在接收收 ...

  2. Vue中非父子组件传值的问题

    父子组件传值的问题,前面已经讲过,不再叙述,这里来说一种非父子组件的传值. vue官网指出,可以使用一个空vue实例作为事件中央线! 也就是说 非父子组件之间的通信,必须要有公共的实例(可以是空的), ...

  3. Vue非父子组件传值

    <template> <div id="app"> <v-home></v-home> <br> <hr> ...

  4. vue 中父子组件传值:props和$emit

    更新----------- 1 父组件向子组件传值:通过props数组: 在vue-cli Login.vue父组件中有AcceptAndRefuse.vue子组件,首先import进子组件hello ...

  5. vue中父子组件传值问题 通过props 和 $emit()方法

    (代码在最后) 1.父组件给子组件传值直接通过props,听着很简单,但是对于初学者来说还是比较难以理解的,今天小白通过自己的实践操作结合代码分析一下 案例  把模态框单独的抽离出来,当作一个组件 第 ...

  6. NO--16 vue之父子组件传值

    先创建项目并运行 vue init webpack-simple templatecd templatenpm inpm run dev 一.子组件访问父组件的数据 方式一 :子组件直接访问父组件的数 ...

  7. Vue组件传值(二)之 非父子组件传值

    Vue中非父子组件之间是如何实现通信的? 本章主要讲的是非父子组件传值,父子组件传值请看上一篇文章. 1.创建新的Vue实例引入项目中,通过$emit.$on来实现非父子组件传值: 1 <!DO ...

  8. 【vue】父组件主动调用子组件 /// 非父子组件传值

    一  父组件主动调用子组件: 注意:在父组件使用子组件的标签上注入ref属性,例如: <div id="home"> <v-header ref="he ...

  9. vue父子组件传值加例子

    例子:http://element-cn.eleme.io/#/zh-CN/component/form         上进行改的 父传子:用prop:子组件能够改变父组件的值,是共享的,和父操作是 ...

随机推荐

  1. CSS创意与视觉表现

    视觉效果 CSS代码: .cover { padding: 36% 50%; background: linear-gradient(to right, white 50%, black calc(5 ...

  2. Educational Codeforces Round 73 (Rated for Div. 2)F(线段树,扫描线)

    这道题里线段树用来区间更新(每次给更大的区间加上当前区间的权重),用log的复杂度加快了更新速度,也用了区间查询(查询当前区间向右直至最右中以当前区间端点向右一段区间的和中最大的那一段的和),也用lo ...

  3. Spring AOP 中 advice 的四种类型 before after throwing advice around

    spring  AOP(Aspect-oriented programming) 是用于切面编程,简单的来说:AOP相当于一个拦截器,去拦截一些处理,例如:当一个方法执行的时候,Spring 能够拦截 ...

  4. float元素上-margin的用法

    css标准,float元素上的负margin表示把下面的元素向对应方向移动,并且覆盖上一个元素(这里是指html中元素的声明顺序). 标准情况下我们用float 时候是这样的. -margin通俗点说 ...

  5. 线程池三种队列使用,SynchronousQueue,LinkedBlockingQueue,ArrayBlockingQueue

    使用方法: private static ExecutorService cachedThreadPool = new ThreadPoolExecutor(4, Runtime.getRuntime ...

  6. oracle查询连续n天登录的用户

    -- 查询连续3天登录的用户 1 先创建一个表,如下: create table USER_DATA ( USER_ID NUMBER, LOGIN_TIME DATE ); 2 插入用户登录数据: ...

  7. HDU 2680 最短路 迪杰斯特拉算法 添加超级源点

    Choose the best route Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  8. kvm的分层控制

    第五层 virsh virt-manager(和libvirtd)利用了libvirt-api   virsh pool-list --all   virsh pool-define xxx/xml会 ...

  9. 吴裕雄--天生自然PythonDjangoWeb企业开发:框架基础和技术选型

    简单的Web Server import socket eol1 = b'\n\n' eol2 = b'\n\r\n' body = '''Hello,world!<h1>tszrwyx& ...

  10. 最简单、最常用的一些Git命令

    ---------------------------------------------------------------------------------------------------- ...