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

使用步骤:

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

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

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

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

在父组件data定义height属性; 在父组件中实现getData(height)方法,方法参数是子组件传递的数据,例如这里直有一个height,然后为this.height赋值; 赋值完毕后就可以使用了;

初始时在子组件中定义width和height

通过一个button来进行实现子组件向父组件进行传值

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>子组件向父组件传值</title>
<script type="text/javascript" src="../js/vue.js" ></script>
</head>
<body>
<div>
<father-component ></father-component>
</div>
</body>
<template id="father-template">
<div>
<h2> 父组件</h2>
myData:<span>{{name}},
{{id}},
{{user.username}},{{age}}
</span><br />
childData:<span> width:{{width}}</span><br /><span>height:{{height}}</span> <hr />
<child-component :id='id' :age='age' @sent-event='getData'></child-component>
</div>
</template>
<template id="child-template">
<div>
<p> 子组件</p>
fatherData:<span >{{name}},{{id}},{{user.username}},{{age}}</span><br />
myData:<span> width:{{width}}</span><br /><span>height:{{height}}</span><br />
<button @click="setData">向父组件传送数据</button> </div>
</template>
<script> new Vue({ data:{ }, components:{
"father-component":{ methods:{ getData(width,height){
this.width=width;
this.height=height;
}
},
data(){
return{
id:"",
name:'perfect', user:{ username:'博客园---perfect*',
password:'' },
age:,
width:,
height:
}
}, template:'#father-template', components:{
"child-component":{ methods:{
setData(){
console.log(this);//这里的this指的是child-component实例
this.$emit('sent-event',this.width,this.height);
}
},
data(){ return{
width:,
height:
}
}, template:'#child-template', props:{ name:{
type:String,
//required:true,//必须进行传值 default:'perfect*'//定义一个默认值
},
id:[Number,String],
user:{
type:Object,
default:function(){
return {username:'perfect***',password:''}
}
}, age:{
type:Number,
validator: function (value) {
return value>=;
} }
}
}
}, }
} }).$mount('div');
</script>
</html>

最新文章

  1. 2.第一个Struts2程序-HelloWorld程序
  2. eclipse配置tomcat 和JRE环境
  3. man page分類與說明
  4. js文档视口高度函数
  5. &quot;QQ尾巴病毒&quot;核心技术的实现原理分析
  6. linux的HugePage与oracle amm关系
  7. Linux的网卡由eth0变成了eth1,如何修复 (转载)
  8. 【STL源码学习】STL算法学习之一
  9. VLD(Visual LeakDetector)内存泄露库的使用
  10. hdu 2066 一个人的旅行(最短路问题)
  11. 【写一个自己的js库】 4.完善跨浏览器事件操作
  12. 工信部:我国4G用户近9.5亿 用户平均使用流量近2G
  13. java中IO操作
  14. 在MFC中UpdateData()的作用
  15. Java集合类框架的最佳实践有哪些?
  16. Quartz的JobDetail没有触发器指向时会被删除的问题
  17. a or an
  18. python day10作业答案
  19. 用SDWebImage加载FLAnimatedImage
  20. [Python学习]Iterator 和 Generator的学习心得

热门文章

  1. Rabbitmq重启服务器用户丢失解决办法
  2. 怎样将PDF文件转换成Excel表格
  3. python2和python3共存时,设置默认python为python3
  4. CITS1401 Computational Thinking with Python
  5. 搭建docker私有仓库(https)
  6. @CreatedDate、@CreatedBy、@LastModifiedDate、@LastModifiedBy
  7. linux发展
  8. Failed to set MokListRT: Invalid Parameter Something as gone seriously wrong: import_mok_state() failed: Invalid Parameter
  9. table的thead,tbody,tfoot
  10. FB面经 Prepare: Make Parentheses valid