<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>componentKnowledge-同級組件通信</title>
<script src="js/vue.js"></script>
</head>
<body>
<template id="aa">
<div>
I am aa component:{{msg}}
<input type="button" @click="send" value="SendTo-cc">
</div>
</template>
<template id="bb">
<div>
I am bb component:{{msg}}
<input type="button" @click="send" value="SendTo-cc">
</div>
</template>
<template id="cc">
<div>
<div>
I am cc component:{{msg}}/receive data:{{msg2}},{{msg3}}
</div>
</div>
</template>
<script>
window.onload=function(){
let Event=new Vue();
let aa={//定義組件
template:'#aa',
data(){
return {msg:'aa data'}
},
methods:{
send(){
Event.$emit('a-send',this.msg)
}
}
};
let bb={//定義組件
template:'#bb',
data(){
return {msg:'bb data'}
},
methods:{
send(){
Event.$emit('b-send',this.msg)
}
}
};
let cc={//定義組件
template:'#cc',
data(){
return {
msg:'cc data',
msg2:'',
msg3:''
}
},
mounted(){
Event.$on('a-send',(data)=>{this.msg2=data});
Event.$on('b-send',(data)=>{this.msg3=data});
}
};
let vm=new Vue({
el:'#app',
components:{//注冊組件
aa,
bb,
cc
}
});
}
/*同級組件之間的通信關鍵總結:
1:新建一個空的root組件:let Event=new Vue();
其上有兩個方法Event.$emit('a-fnName',data)/Event.$on('a-fnName',(data)=>{})
2:發送數據的組件:Event.$emit('a-fnName',data)寫在組件的methods里,
3:接收數據的組件:Event.$on('a-fnName',(data)=>{}),注意函數格式必須寫為箭頭函數,不然this指向不是當前組件,一般寫在鈎子函數里(beforeCreate(){}/created(){}/beforeMount(){}/Mounted(){}/beforeUpdate(){}/updated(){}/beforeDestroy(){}/destroyed(){})
*/
</script>
<div id="app">
<!--使用組件-->
<aa></aa>
<bb></bb>
<cc></cc>
</div>
</body>
</html>
<!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>
Vue.js組件的通信之子組件向父組件的通信
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>componentChildToParentCommunication</title>
<script src="js/vue.js"></script>
</head>
<template id="parentComp">
<div>
I am parent component:{{msg}},The Data from child:{{msg1}},{{msg2}}
<hr>
<child :m1="msg1" :m2="msg2"></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'
}
},
props:['m1','m2']
};
let parent={
template:'#parentComp',
data(){
return {
mgs:'parent Data',
msg1:'the first parent Data',
msg2:'the second parent Data'
}
},
components:{
child
},
};
window.onload=function(){
new Vue({
el:'#app',
components:{
parent
}
});
}
/*子元素向父元素通信關鍵總結:
1:子元素定義時props:['msg1','msg2','msg3',...],用來放置從父元素拿過來的數據
2:在嵌套的子元素(使用時)上:<child :msg1="父數據1" :msg2="父數據2" :msg3="父數據3"></child>;
*/
</script>
<div id="app">
<parent></parent>
</div>
</body>
</html>