VUE父子通信
一、父调用子的方法
<child @toParent="Parent" ref="child1"></child>
this.$refs.child1.getMsg(str);
- child1:子组件ref绑定的值
- getMsg:子组件的方法
- str:传递的值
二、子调用父的方法
this.$emit("toParent",str)
<child @toParent="Parent"></child>
1.toParent:父组件声明的方法
2.str:传递的值
三、父传子值
父:<child @parentMessage="Message"></child>
子: props: ['parentMessage']
四、兄弟通信
我们可以实例化一个vue实例,相当于一个第三方
let vm = new Vue(); //创建一个新实例
1组件
vm.$emit('blur','sichaoyun'); //触发事件
2组件接受1组件
created() {
vm.$on('blur', (arg) => {
this.test= arg; // 接收
});
}