# 1、父与子通信 (props down)
1.发送
<son myName='zhangsan'>
</son>
2.接受
到son组件:
Vue.component('son',{
props:['myName'],
template:`
<p>{{myName}}</p>
`
})
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
# 2、子与父通信 (events up)
1.绑定
methods:{
handleEvent:function(msg){}
}
<son @customEvent="handleEvent"></son>
2.触发
子组件内部:
this.$emit(‘customEvent’,100);
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
# 3、ref(reference 引用/参考 外号)
帮助在父组件中 得到子组件中的数据、方法。
1.指定ref属性
<son ref="mySon"></son>
2.根据ref得到子组件实例
this.$refs.mySon
1
2
3
4
5
2
3
4
5
# 4、$parent
this.$parent得到父组件的实例
1
# 5、兄弟组件通信
1.var bus = new Vue();
2.接收方
bus.$on('eventName',function(msg){})
3.发送方
bus.$emit('eventName',123);
1
2
3
4
5
2
3
4
5