##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>
父組件向子組件傳數據較爲簡單,子組件用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>
運行結果如圖 this
子組件向父組件傳遞數據用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>
結果以下 還有另外一種方法與兄弟組件通訊方式相同用eventBus
兄弟組件通訊用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>
結果如圖
子組件向父組件傳遞數據也能夠使用這種方法,仿兄弟組件通訊便可
參考文章