Vue.js组件的通信之父组件向子父组件的通信
2017-03-09 19:41
691 查看
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>componentParentChildCommunication</title>
<script src="js/vue.js"></script>
</head>
<template id="parentComp">
<div>
I am parent component:{{msg}},The Data from child:{{msg2}}
<hr>
<!-- <child @自定义事件名="父方法"></child> -->
<child @child="parentFn"></child>
</div>
</template>
<template id="childComp">
<div>I am child component:{{msg}}</div>
</template>
<body>
<script>
let child={
template:'#childComp',
data(){
return {
msg:'child Data'
}
},
mounted(){
/*this.$emit('自定义事件名',数据);*/
this.$emit('child',this.msg);
}
};
let parent={
template:'#parentComp',
data(){
return {
msg:'parent Data',
msg2:''
}
},
components:{
child
},
methods:{
parentFn(data){
this.msg2=data;
}
}
};
window.onload=function(){
new Vue({
el:'#app',
components:{
parent
}
});
}
/*父元素向子元素通信关键总结:
1:在嵌套的子元素(使用时)上:<child @自定义事件名="父方法"></child>;
2:子元素在加载完成的钩子函数(mounted)上加一个方法:this.$emit('自定义事件名',数据);
3:父元素上的方法:父方法名(data){...}
*/
</script>
<div id="app">
<parent></parent>
</div>
</body>
</html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>componentParentChildCommunication</title>
<script src="js/vue.js"></script>
</head>
<template id="parentComp">
<div>
I am parent component:{{msg}},The Data from child:{{msg2}}
<hr>
<!-- <child @自定义事件名="父方法"></child> -->
<child @child="parentFn"></child>
</div>
</template>
<template id="childComp">
<div>I am child component:{{msg}}</div>
</template>
<body>
<script>
let child={
template:'#childComp',
data(){
return {
msg:'child Data'
}
},
mounted(){
/*this.$emit('自定义事件名',数据);*/
this.$emit('child',this.msg);
}
};
let parent={
template:'#parentComp',
data(){
return {
msg:'parent Data',
msg2:''
}
},
components:{
child
},
methods:{
parentFn(data){
this.msg2=data;
}
}
};
window.onload=function(){
new Vue({
el:'#app',
components:{
parent
}
});
}
/*父元素向子元素通信关键总结:
1:在嵌套的子元素(使用时)上:<child @自定义事件名="父方法"></child>;
2:子元素在加载完成的钩子函数(mounted)上加一个方法:this.$emit('自定义事件名',数据);
3:父元素上的方法:父方法名(data){...}
*/
</script>
<div id="app">
<parent></parent>
</div>
</body>
</html>
相关文章推荐
- Vue.js每天必学之组件与组件间的通信
- 七周七种前端框架四:Vue.js 组件和组件通信
- vue子父组件通信的实现代码
- [js高手之路]Vue2.0基于vue-cli+webpack父子组件通信教程
- [js高手之路]Vue2.0基于vue-cli+webpack父子组件通信教程
- 深入探讨Vue.js组件和组件通信
- Vue2.0子父组件通信
- vue.js学习笔记(三)--父子组件通信总结
- Vue.js组件之间的通信
- [js高手之路]Vue2.0基于vue-cli+webpack父子组件通信教程
- Vue.js学习 Item11 – 组件与组件间的通信
- Vue.js-----轻量高效的MVVM框架(十、父子组件通信)
- Vuejs——(10)组件——父子组件通信
- Vue.js 组件和组件通信
- Vuejs第十篇之vuejs父子组件通信
- vue.js入门(3)——组件通信
- vue.js组件与组件之间的通信
- Vue.js组件之同级之间的通信
- [js高手之路]Vue2.0基于vue-cli+webpack同级组件之间的通信教程
- Angularjs Controller 间通信方式、vue组件之间通信