首页 > 其他 > 详细

vue中兄弟之间组件通信

时间:2019-02-18 18:20:15      阅读:195      评论:0      收藏:0      [点我收藏+]

我们知道Vue中组件之间的通信有很多方式,父子之间通信比较简单,当我们使用vuex时候,兄弟组件之间的通信也很好得到解决

当我们项目较小时候,不使用vuex时候Vue中兄弟组件之间的通信是怎样进行的呢

参考链接:https://my.oschina.net/u/3229305/blog/1820279

//在生成vue实例前,给Vue的原型上添加一个bus属性,这个属性是vue的实例,
//之后创建的vue实例都具有bus这个属性
//首先在main.js
 Vue.prototype.bus = new Vue();

//组件hello

<template>
  <div class="container">
    <button @click="handler">hello word</button>
    <word></word>
  </div>
</template>
<script>
  import word from ‘./word.vue‘
  export default{
    methods:{
      handler () {
        this.$bus.$emit(‘shareText‘, ‘hello word‘)
      }
    }
  }
</script>

// 组件world
<template>
  <div class="con">
    {{text}}
  </div>
</template>
<script>
export default {
  data () {
    return {
      text: ‘hello‘
    }
  },
  mounted () {
    var that = this
    this.$bus.$on(‘shareText‘, function (text) {
      that.text = text
    })
  }

}
</script>

vue中兄弟之间组件通信

原文:https://www.cnblogs.com/jkingdom/p/10393219.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!