vue 通信

107 阅读1分钟

持续创作,加速成长!这是我参与「掘金日新计划 · 6 月更文挑战」的第12天,点击查看活动详情

vue组件间通信分为以下几种:

  • 父向子传递数据,用自定义属性
  • 子向父传递数据,用自定义事件
  • 兄弟(任意)组件间传递数据,用全局事件总线或者消息订阅与发布

1.父向子传递数据

前面已经说了,父向子传递数据用的是自定义属性,接下来就让我们看下代码是怎么写的

  • 父组件
<template>
  <div class="father">
    <!-- 父组件向子组件传递person对象 -->
    <Son :person="person"/>
  </div>
</template>
<script>
export default {
    data (){
        return {
            person:{name:'张三',age:18,sex:'男'}
        }
    }
}
</script>

  • 子组件
<template>
  <div class="son">
      <h2>我是子组件</h2>
      <div>
        <h4>个人信息展示</h4>
		<ul>
            <li><label>姓名:</label><span>{{person.name}}</span></li>
            <li><label>年龄:</label><span>{{person.age}}</span></li>
            <li><label>性别:</label><span>{{person.sex}}</span></li>
        </ul>
      </div>
  </div>
</template>
<script>
export default {
  //子组件通过props接收父组件传递过来的数据
  props:['person']
}
</script>


1.props的大小写

当我们使用自定义属性传递数据的时候,自定属性的名称可能不会简单的是一个单词,那这个时候我们该如何书写呢?请看如下代码

//父组件 父组件传递了 company-name
<Son company-name = "Microsoft"></Son>
//子组件 子组件接收时的写法
<script>
export default {
    props:['companyName']
}
</script>

2.props的两种写法

props:{
    name:{
        type:String,
        required:true,
        default:''
    },
    age:{
        type:Number,
        required:true,
        default:0
    },
    sex:String
}

3.传递动态props


<Son :categoryList="categoryList"></Son>
<script>
export default {
	data (){
        return {
            //购物车列表数据
            categoryList:[]
        }
    }        
}
</script>

2.子向父传递数据

前面讲到,子向父传递数据需要用到自定义事件,但是这里通过自定义属性也可以实现,我们一起来看一下

<Son :categoryList="categoryList"></Son>
<script>
export default {
	data (){
        return {
            //购物车列表数据
            categoryList:[]
        }
    }        
}
</script>

//子组件 Son.vue
<template>
  <div class="son">
    <button @click="sendMsgForFather">发送信息</button>
  </div>
</template>
<script>
export default {
  props:['getSonMessage'],
  methods:{
    //发送信息给父组件
    sendMsgForFather(){
      this.$emit('eventN',`我是子组件,hello`)
    }
  }
}
</script>

其实理解起来还是很简单的,给子组件上绑定一个自定义事件,子组件上的自定义事件通过$emit触发,而$emit通过子组件上的按钮点击事件来触发,最终将数据发送给父组件,父组件通过demo函数拿到并展示数据,估计听完我说的,肯定蒙了。研究一下代码,自己敲一下就明白了

3.兄弟(任意)组件间的传值

兄弟组件间的传值方法比较多,包括我们甚至可以通过逐层使用自定义属性自定义事件来实现,但代码看起来可能不是那么舒服,甚至把自己都给绕晕了,我自己也试过,想想这种方法知道就行,效率太低了。接下来就讲讲目前主流的几种兄弟组件间传值的方法

3.1全局事件总线

//main.js中安装全局事件总线
new Vue({
  render: h => h(App),
  beforeCreate(){
    Vue.prototype.$bus = this //安装全局事件总线
  }
}).$mount('#app')

3.2消息订阅与发布

//消息订阅者(接收方) Subscribe.vue
<template>
  <div class="subscribe">
  </div>
</template>
<script>
import pubsub from 'pubsub-js'
export default {
    mounted(){
        this.pubId = pubsub.subscribe('message',(msgName,data)=>{
            console.log(`我收到了消息${msgName},内容是${data}`);
        })
    },
    beforeDestroy(){
        pubsub.unsubscribe(this.pubId)
    }
}
</script

//消息发布者(发送方) Publish.vue
<template>
  <div class="publish">
      <button @click="sendMsg">点击发送</button>
  </div>
</template>
<script>
import pubsub from 'pubsub-js'
export default {
    methods:{
        sendMsg(){
            pubsub.publish('message','这是订阅的消息')
        }
    }
}
</script>