Vue组件  传送门

  子组件向父组件传值:子组件通过$.emit()方法以事件形式向父组件发送消息传值;

  使用步骤:

    1、定义组件:现有自定义组件com-a、com-b,com-a是com-b的父组件;

    2、准备获取数据:父组件com-a要获取子组件data中的height属性;

    3、在子组件com-b中,需要用$.emit()方法将数据以事件的形式发送,$.emit('sendData', data, data…),红色的部分事件名可自定义,数据可传递多个;

    4、在父组件中使用子组件的地方 <com-b @自定义事件名='getData'></com-b> 监听子组件自定义的事件,并且在方法中获取数据;

    5、在父组件data定义height属性;

    6、在父组件中实现getData(height)方法,方法参数是子组件传递的数据,例如这里直有一个height,然后为this.height赋值;

    7、赋值完毕后就可以使用了;

 

  Learn

    一、子组件向父组件传值

  目录结构

  

一、子组件向父组件传值

  在子组件中初始化数据

  "child-component" : {
template : "#child-template",
methods : {
sendData(){
console.log(this);
this.$emit('send-event', this.width, this.height);
}
},
data(){
return {
width : 50,
height : 100
}
},
props : {
name : {
type : String,
//required : true,
default : "Garydef"
},
id : [Number, String],
user : {
type : Object,
default : function(){
return {username : 'lain', password : '123123'};
}
},
age : {
type : Number,
validator : function(value){
return value >= 0;
}
}
}
}

  在子组件中添加<button>按钮,并绑定sendData点击事件,sendData函数在子组件中已经绑定

<button @click="sendData">向父组件发送数据</button>

  父组件中直接获得子组件id和age属性值

<child-component  :id="id" :age="age" @send-event="getData"></child-component>
    <template id="father-template">
<div>
<h1>father component</h1>
myData :
<span>
{{name}} ,
{{id}} ,
{{user.username}} ,
{{age}}
</span><br />
childData :
<span>
{{width}},
{{height}}
</span><hr />
<child-component :id="id" :age="age" @send-event="getData"></child-component>
</div>
</template> <template id="child-template">
<div>
<h2>child component</h2>
fatherData :
<span>
{{name}} ,
{{id}} ,
{{user.username}},
{{age}}
</span><br />
myData :
<span>
{{width}},
{{height}}
</span><br />
<button @click="sendData">向父组件发送数据</button>
</div>
</template>

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Gary</title>
</head>
<body>
<div id="GaryId">
<father-component></father-component>
</div>
</body> <template id="father-template">
<div>
<h1>father component</h1>
myData :
<span>
{{name}} ,
{{id}} ,
{{user.username}} ,
{{age}}
</span><br />
childData :
<span>
{{width}},
{{height}}
</span><hr />
<child-component :id="id" :age="age" @send-event="getData"></child-component>
</div>
</template> <template id="child-template">
<div>
<h2>child component</h2>
fatherData :
<span>
{{name}} ,
{{id}} ,
{{user.username}},
{{age}}
</span><br />
myData :
<span>
{{width}},
{{height}}
</span><br />
<button @click="sendData">向父组件发送数据</button>
</div>
</template> <script type="text/javascript" src="../js/vue.js" ></script>
<script type="text/javascript"> new Vue({
data : { },
components : {
"father-component" : {
methods : {
getData(width, height){
this.width = width;
this.height = height;
}
},
data(){
return {
id : '01',
name : 'Gary',
user : {
username : 'userGary',
password : 'pw123'
},
age : 18,
width : 0,
height : 0
}
},
template : "#father-template",
components : {
"child-component" : {
template : "#child-template",
methods : {
sendData(){
console.log(this);
this.$emit('send-event', this.width, this.height);
}
},
data(){
return {
width : 50,
height : 100
}
},
props : {
name : {
type : String,
//required : true,
default : "Garydef"
},
id : [Number, String],
user : {
type : Object,
default : function(){
return {username : 'lain', password : '123123'};
}
},
age : {
type : Number,
validator : function(value){
return value >= 0;
}
}
}
}
}
}
}
}).$mount("#GaryId"); </script>
</html>

Gary_childToFather.html

Vue_(组件通讯)子组件向父组件传值的更多相关文章

  1. vue 子组件把数据传递给父组件

    <div id="app"> <child v-on:drop='parent'></child> //这里v-on:drop="pa ...

  2. vue子组件使用自定义事件向父组件传递数据

    使用v-on绑定自定义事件可以让子组件向父组件传递数据,用到了this.$emit(‘自定义的事件名称’,传递给父组件的数据) <!DOCTYPE html> <html lang= ...

  3. Vue的父子组件v-model双向绑定,父组件修改子组件中绑定的v-model属性

    先来看下实现的效果,父组件中有个文本框,在点击下面按钮时弹出抽屉,抽屉里也有个文本框,文本框里的初始值要和父组件的文本框同步,并且修改抽屉里的文本框值时 父组件里的文本框值也要跟着改变 网上有大概三种 ...

  4. vue 子页面,向父页面 传值...

    子组件 通过 事件 向父组件传值..... 父组件 方法: methods: { appendData: function (list) { console.log(list); for (var i ...

  5. layui type:2 iframe子页面向父页面传值

    需求: 选择子页面表格中的radio或者双击该行,得到的该行数据传到父页面,由父页面渲染. 网上的各种方法都用了,父页面就是获取不到子页面传的值,过了一晚上,睡了一觉,柳暗花明又一村. layui t ...

  6. Vue 组件&组件之间的通信 之 父组件向子组件传值

    父组件向子组件传值:父组件通过属性向下传值的方式和子组件通信: 使用步骤: 定义组件:现有自定义组件com-a.com-b,com-a是com-b的父组件: 准备获取数据:com-b要获取父组件dat ...

  7. 【前端vue开发】vue子调父 $emit (把子组件的数据传给父组件)

    ps:App.vue 父组件 Hello.vue 子组件 <!--App.vue :--> <template> <div id="app"> ...

  8. vue $emit 父组件与子组件之间的通信(父组件向子组件传参)

    1.首先新建一个子页面为 env.vue的文件(名字这里大家可以自取) 2.然后把子页面引入父页面,代码如图: import env from '@/components/common/env' ex ...

  9. reac——父组件向子组件传递值,子组件何时能同步获得父组件改变后的值

    //这里是父组件的代码:export default class HeaderCom_son extends React.Component { constructor(props) { super( ...

随机推荐

  1. 异常-Throwable的几个常见方法

    package cn.itcast_04; import java.text.ParseException; import java.text.SimpleDateFormat; import jav ...

  2. Resource通配符路径 ——跟我学spring3

    转自: https:// jinnianshilongnian.iteye.com/blog/1416322

  3. C# 中 ContextMenuStrip 和 ContextMenu区别

    简单来说,就是版本不同,只不过是升级后建议功能更加强大的ContextMenuStrip罢了,升级后的元件功能更强 . ContextMenu是VS2005里的,而ContextMenuStrip是V ...

  4. beego中获取url以及参数的方式

    以下都全默认在controller下执行 获取当前请求的referer fmt.Println(this.Ctx.Request.Referer()) 输出:http://localhost:8080 ...

  5. Mount Windows (CIFS) shares on Linux with credentials in a secure way

      Posted on 09/09/2014 In almost all cases, when mounting a CIFS-share on a Linux host, you will nee ...

  6. 4、linux目录结构

    一.目录结构 /: 所有linux操作系统的顶点目录,不像windows,每个分区都有一个顶点目录 /boot 存放系统启动时相关的文件,比如kernel内核,grub引导菜单.(不要删除.) /bi ...

  7. 微软宣布加入机密计算联盟,与谷歌和BAT 等巨头联手保护数据安全

    联盟创始成员还包括阿里巴巴.Arm.百度.谷歌.IBM.英特尔.红帽.瑞士电信和腾讯等科技公司,它提供了一个让行业聚集起来的机会,以促进使用机密计算来更好地保护数据. 建立机密计算联盟的需求源于这样一 ...

  8. C语言-数字字符串转换成这个字符串对应的数字(十进制、十六进制)

    数字字符串转换成这个字符串对应的数字(十进制.十六进制) (1)数字字符串转换成这个字符串对应的数字(十进制) 要求:这个字符串参数必须包含一个或者多个数字,函数应该把这些数字转换为整数并且返回这个整 ...

  9. C#在Oralce环境执行查询时报"Arithmetic operation resulted in an overflow"

    问题描述:C#代码在Oralce环境执行分组求和的Sql时报错,提示“Arithmetic operation resulted in an overflow”,即算术运算导致溢出 (1).执行Sql ...

  10. 【安徽集训】Emerald

    Description \(n\) 座城市在数轴上,第 \(i\) 座城市有一条连向第 \(i+1\) 座城市的单向边.每座城市有一个类型 A/B 以及一个非负整数人口,A 类城市的人觉得自己的城市比 ...