组件通讯

71 阅读1分钟

组件通讯

Vue 组件间通信只要指以下 3 类通信:父子组件通信、隔代组件通信、兄弟组件通信,下面我们分别介绍每种通信

方式且会说明此种方法可适用于哪类组件间通信。

父传子

<child :user="{name:'jack'}"></child>

child

```
props:['user']
props:{
    user:Object
}
props:{
    user:{
        type:Object,
        defaule:{}
    }
}
```

子传父

     <child @some-event="callback"></child> 
     child 
     this.$emit('someEvent',参数) 
     触发  parent callback(n){
     }

兄弟 vue2 事件总线

    const vue = new Vue() brother1 //侦听sendmsg方法,执行回调函数
    vue.$on('sendMsg',function(){
    })

    broter2 vue.$emit('sendMsg',参数)

    P A B
    ref <child ref="child"> parent this.$refs.child