vue2.0父子组件以及非父子组件如何通信

父子组件通信其实就是 props 和 $emit 的使用

  总结就是:父子组件通信,父组件传递数据给子组件,子组件触发父组件方法并传递父组件数据,通过props 和 $emit 来实现;非父子组件通信,就是通过自定义事件来实现,不过Vue event自带了这个属性,所以直接通过 event.$emit 和 event.$on 来实现

一、父组件传递数据给子组件

父组件数据如何传递给子组件呢?可以通过props属性来实现

父组件:

<parent>
    <child :child-msg="msg"></child>//这里必须要用 - 代替驼峰
</parent>

data(){
    return {
        msg: [1,2,3]
    };
}

子组件通过props来接收数据: 
方式1:

props: ['childMsg']

方式2 :

props: {
    childMsg: Array //这样可以指定传入的类型,如果类型不对,会警告
}

方式3:

props: {
    childMsg: {
        type: Array,
        default: [0,0,0] //这样可以指定默认的值
    }
}

这样呢,就实现了父组件向子组件传递数据.

具体子组件方法

<template>
  <div>
    子组件:
    <span>{{inputName}}</span>
  </div>
</template>
<script>
  export default {
    // 接受父组件的值
    props: {
      inputName: String,
      required: true
    }
  }
</script>

子组件与父组件通信

那么,如果子组件想要改变数据呢?这在vue中是不允许的,因为vue只允许单向数据传递,这时候我们可以通过触发事件来通知父组件改变数据,从而达到改变子组件数据的目的.

子组件:
<template>
    <div @click="up"></div>
</template>

methods: {
    up() {
        this.$emit('upup',{
       type: id,
       model: itemT.DICTIONARIES_ID

主动触发upup方法,'hehe'为向父组件传递的数据

    }
}

父组件:

<div>
    <child @upup="change"></child> //监听子组件触发的upup事件,然后调用change方法 直接在这里打印数据就可以了
</div>
methods: {
    change(date) {
        this.date = date;
    }
}

三、非父子组件通信

如果2个组件不是父子组件那么如何通信呢?这时可以通过eventHub来实现通信. 
所谓eventHub就是创建一个事件中心,相当于中转站,可以用它来传递事件和接收事件. 新建一个 event.js文件,在需要通信的两个组件中引入这个 js文件

import Vue from 'vue'
export default new Vue(); //创建事件中心

组件1:通过 event.$emit 触发

<div @click="eve"></div>

// 引入 event
import event from './event'
methods: { eve() { event.$emit('change','hehe'); //Hub触发事件 } }

组件2: 通过event.$on 接收

<div></div>
created() {
    event.$on('change', () => { //Hub接收事件,也就是绑定自定义事件
        this.msg = 'hehe';
    });
  // 假设这里调用了另外一个函数处理返回数据,这个时候就要及时销毁这个 changeFun
  event.$on('change', this.changeFun);
}

销毁

beforeDestory() {
            event.$off('change', this,changeFun)
        }

这样就实现了非父子组件之间的通信了.原理就是把Hub当作一个中转站!也就是自定一了一个事件,不过这个事件是vue自带了(也就是event.$on event.$emit )所以不用自己在定义事件了;不过这中方法,要及时销毁自定义事件,否则会产生内存泄露;

四、父组件于子组件通信解决了,但是如果子组件要有操作:

父组件

 <!-- 
            model
            定义一个 方法事件 close 调用 closeModel 这个函数
        定义一个 sure 方法,在这次的组件调用中,执行 gotoCart 函数,执行什么,方法中写,sure方法具体在子组件中哪里调用,就看@click写字组件的哪里。
model组件可以被多次调用在一个页面的,假如又有一个地方调用了这个组件,这次 确认叫做,查看购物车,那么这里就是要执行跳转了,sure 这个方法不变,还是定义sure方法,因为
这个方法,在组建中建立了,这里吧sure方法对赢执行的函数,重新一个就行,相当于 sure 方法只是一个 父子组件 的中间商,起到通信的作用,真正起作用的是这个方法
对应的函数
例如 加入购物车失败


注意:子组件 父组件 绑定对应的函数必须在最外层,子组件的确认按钮执行函数,再确认按钮上,在父组件中没有确认按钮的,父组件只是一个插槽
--> <Model v-bind:mdShow="mdShow" v-on:close="closeModel" v-on:sure="gotoCart"> <span slot="message">加入购物车成功</span> <span slot="leftMessage">取消</span> <span slot="rightMessage">确认</span> </Model>

<!-- 加入购物车失败 -->
<Model v-bind:mdShow="mdShowFail" v-on:close="closeModel" v-on:sure="chenkCart">
  <span slot="message">{{messageNew}}</span>
  <span slot="leftMessage">取消</span>
  <span slot="rightMessage">查看购物车</span>
</Model>



closeModel() {
  this.mdShow = false; // 执行要执行的
}

gotoCart(){

}
chenkCart(){

}

子组件

<template>
    <div class="model_container" v-show="mdShow == true"  @click="closeModel">
        <div class="model_wrap">
            <div class="text">
                <slot name="message"></slot>

            </div>
            <!-- <p>{{mdShow}}</p> -->
            <div class="bottom">
                <Button class="right_bd" @click="closeModel">
                    <slot name="leftMessage"></slot>
                </Button>
                <Button type="primary" @click="sureModel">
                    <slot name="rightMessage"></slot>
                </Button>
            </div>
        </div>
    </div>
</template>

methods: {
        closeModel() {
            // 调用父组件的 close 方法
            this.$emit("close");
        },
        // 调用父组件的 sure 方法

      sureModel(){
        this.$emit("sure");
      }

    }

就是性当与是,子组件通知父组件 我操作了,你在父组件该执行你要执行的了