[TOC]
父子组件通信
父传子
- 通过props来进行通信 ```vue <!DOCTYPE html>
 
<a name="M6DO2"></a>
### 子传父
子组件通过this.$emit方法传出
```vue
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <div id="app">
        <!-- 3.使用子组件 -->
        <App></App>
    </div>
    <script src="./vue.js"></script>
    <script>
        // 全局组件
        // 子往父传值
        // 在父组件中 子组件上绑定自定义事件
        // 在子组件中 触发原生的事件 在事件函数通过this.$emit触发自定义的事件
        Vue.component('Child', {
            template: `
                <div>
                    <h3>我是一个子组件</h3>   
                    <h4>{{childData}}</h4>
                    <input type="text" @input = 'handleInput'/>
                </div>
            `,
            props: ['childData'],
            methods:{
                handleInput(e){
                    const val = e.target.value;
                    this.$emit('inputHandler',val);
                }
            },
        })
        const App = {
            data() {
                return {
                    msg: '我是父组件传进来的值',
                    newVal:''
                }
            },
            methods:{
                input(newVal){
                    // console.log(newVal);
                    this.newVal = newVal;
                }
            },
            template: `
                <div>
                    <div class='father'>
                        数据:{{newVal}}
                    </div>
                    <Child :childData = 'msg' @inputHandler = 'input'></Child>
                </div>
            `,
            computed: {
            }
        }
        new Vue({
            el: '#app',
            data: {
            },
            components: {
                // 2.挂载子组件
                App
            }
        })
    </script>
</body>
</html>
平行组件通信
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <div id="app">
        <!-- 3.使用子组件 -->
        <App></App>
    </div>
    <script src="./vue.js"></script>
    <script>
        const bus = new Vue();
        // 中央事件总线 bus
        Vue.component('B', {
            data() {
                return {
                    count: 0
                }
            },
            template: `
                <div>{{count}}</div>
            `,
            created(){
                // $on 绑定事件
                bus.$on('add',(n)=>{
                    this.count+=n;
                })
            }
        })
        Vue.component('A', {
            data() {
                return {
                }
            },
            template: `
                <div>
                 <button @click='handleClick'>加入购物车</button> 
                </div>
            `,
            methods:{
                 handleClick(){
                    // 触发绑定的函数 // $emit 触发事件
                     bus.$emit('add',1);
                 }
            }
        })
        const App = {
            data() {
                return {
                }
            },
            template: `
                <div>
                    <A></A>
                    <B></B>
                </div>
            `,
        }
        new Vue({
            el: '#app',
            data: {
            },
            components: {
                // 2.挂载子组件
                App
            }
        })
    </script>
</body>
</html>
其它通信方式
上级传下级:支持一级或多级
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <div id="app">
        <!-- 3.使用子组件 -->
        <App></App>
    </div>
    <script src="./vue.js"></script>
    <script>
        // 插槽 留坑
        // props 验证规则
        // 组件的生命周期
        // UI库 element-ui
        // **** 如何设计组件? *****
        // provide
        // inject
        // 父组件 provide来提供变量,然后再子组件中通过inject来注入变量.无论组件嵌套多深
        // 中央事件总线 bus
        Vue.component('B', {
            data() {
                return {
                    count: 0
                }
            },
            inject:['msg'],
            created(){
                console.log(this.msg);
            },
            template: `
                <div>
                    {{msg}}
                </div>
            `,
        })
        Vue.component('A', {
            data() {
                return {
                }
            },
            created(){
                // console.log(this.$parent.$parent);
                // console.log(this.$children);
                console.log(this);
            },
            template: `
                <div>
                     <B></B>
                </div>
            `
        })
        const App = {
            data() {
                return {
                    title:"老爹"
                }
            },
            provide(){
                return {
                    msg:"老爹的数据"
                }
            },
            template: `
                <div>
                    <A></A>
                </div>
            `,
        }
        new Vue({
            el: '#app',
            data: {
            },
            components: {
                // 2.挂载子组件
                App
            }
        })
    </script>
</body>
</html>
                    