Vue之父子兄弟组件间通讯

##Vue之父子兄弟组件间通讯 @(JavaScript)[学习笔记]javascript


[TOC]vue


导言

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

<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>

建立字组件Aapp

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

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

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

建立子组件Bdom

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

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

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

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

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

<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>

子组件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>

运行结果如图 enter image description herethis

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

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

<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>

父组件Appcode

<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>

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

3. 兄弟组件间通讯

兄弟组件通讯用eventBus来实现 新建一个js文件,来建立出咱们的eventBus,把它命名为bus.js 在组件A和组件B导入就能够使用了 bus.js

import Vue from 'vue'
export default new Vue()

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>

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>

结果如图 enter image description here

子组件向父组件传递数据也能够使用这种方法,仿兄弟组件通讯便可

参考文章

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

相关文章
相关标签/搜索