Vue2.0 之父子兄弟组件间通信

转自:http://blog.csdn.net/qq_27346299/article/details/78540751#vue之父子兄弟组件间通信

导言

最近在写个人简历网页版遇到一个问题,想要将一个组件的dom结构传递给其他兄弟组件,然后进行dom操作,不知怎么在其间传递数据,查阅资料,找到解决方法,现记录如下,整理思路加强学习,同时便于他人参考 
创建一个App.vue作为父组件

<template>
  <div class="app">
    <childA></childA>
    <childB></childB>
  </div>
</template>

<script type="text/ecmascript-6">
  import childA from './components/A.vue'
  import childB from './components/B.vue'

  export default {
    components: {
      childA,
      childB
    }
  }
</script>

<style lang="stylus" rel="stylesheet/stylus">
</style>

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

创建字组件A

<template>
  <div class="child-a"></div>
</template>

<script type="text/ecmascript-6">
  export default {}
</script>

<style lang="stylus" rel="stylesheet/stylus">
</style>

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

创建子组件B

<template>
  <div class="child-b"></div>
</template>

<script type="text/ecmascript-6">
  export default {}
</script>

<style lang="stylus" rel="stylesheet/stylus">
</style>

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

1. 父组件向子组件传递消息

父组件向子组件传数据较为简单,子组件用props接收 
父组件App

<template>
  <div class="app">
    <childA :msgApp="msgApp"></childA>
    <childB></childB>
  </div>
</template>

<script type="text/ecmascript-6">
  import childA from './components/A.vue'
  import childB from './components/B.vue'

  export default {
    data () {
      return {
        msgApp: '我是来子父组件的消息'
      }
    },
    components: {
      childA,
      childB
    }
  }
</script>

<style lang="stylus" rel="stylesheet/stylus">
</style>
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

子组件A

<template>
  <div class="child-a">我是组件A,接收到消息:{{msgApp}}</div>
</template>

<script type="text/ecmascript-6">
  export default {
    props: {
      msgApp: {
        type: String
      }
    }
  }
</script>

<style lang="stylus" rel="stylesheet/stylus">
</style>

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

运行结果如图 
enter image description here

2. 子组件向父组件传递数据

子组件向父组件传递数据用this,$emit() 
子组件B

<template>
  <div class="child-b"></div>
</template>

<script type="text/ecmascript-6">
  export default {
    data () {
      return {
        msgB: '我是来子B组件的消息'
      }
    },
    mounted () {             // 这里我选择加载完成就传递数据,也可以定义事件等
      this.$emit('msg', this.msgB)
      console.log(this.msgB)
    }
  }
</script>

<style lang="stylus" rel="stylesheet/stylus">
</style>
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

父组件App

<template>
  <div class="app">
    <childA :msgApp="msgApp"></childA>
    <childB v-on:msg="show"></childB
    我是父组件,接受到消息:{{msgB}}
  </div>
</template>

<script type="text/ecmascript-6">
  import childA from './components/A.vue'
  import childB from './components/B.vue'

  export default {
    data () {
      return {
        msgApp: '我是来子父组件的消息',
        msgB: ''
      }
    },
    methods: {
      show (a) {
        this.msgB = a
      }
    },

    components: {
      childA,
      childB
    }
  }
</script>

<style lang="stylus" rel="stylesheet/stylus">
</style>
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34

结果如下 
enter image description here 
还有另一种方法与兄弟组件通信方式相同用eventBus

3. 兄弟组件间通信

兄弟组件通信用eventBus来实现 
新建一个js文件,来创建出我们的eventBus,把它命名为bus.js 
在组件A和组件B导入就可以使用了 
bus.js

import Vue from 'vue'
export default new Vue()
 
 
  • 1
  • 2

A组件

<template>
  <div class="child-a">我是组件A,接收到B消息:{{msgFromB}}</div>
</template>

<script type="text/ecmascript-6">
  import bus from '../bus'
  export default {
    data () {
      return {
        msgFromB: ''
      }
    },
    mounted () {
      bus.$on('msg', (a) => {
        this.msgFromB = a
      })
    }
  }
</script>

<style lang="stylus" rel="stylesheet/stylus">
</style>
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

B组件

<template>
  <div class="child-b">
  </div>
</template>

<script type="text/ecmascript-6">
  import bus from '../bus'
  export default {
    data () {
      return {
        msgB: '我是来子B组件的消息'
      }
    },
    mounted () {
      bus.$emit('msg', this.msgB)
    }
  }
</script>

<style lang="stylus" rel="stylesheet/stylus">
</style>
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

结果如图 
enter image description here

子组件向父组件传递数据也可以使用这种方法,仿兄弟组件通信即可

参考文章

vue.js之路(4)——vue2.0s中eventBus实现兄弟组件通信

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值