vue 父傳子,子傳父,兄弟組件之間的傳遞方式

在小型項(xiàng)目當(dāng)中,如果不考慮VueX,組件之間的通信該如何處理,接下來我們創(chuàng)建parents childA childB 三個(gè)組件演示如何傳遞與接收

parents組件:

<template>
  <div>
    <childA/>
    <childB/>
  </div>
</template>

<script>
export default {
  components: {
    childA: () => import('@/components/childA'),
    childB: () => import('@/components/childB'),
  },
}
</script>

childA組件:

<template>
  <div>
    <h2>Hello,我是ChildA組件</h2>
  </div>
</template>

<script>
export default {

}
</script>

childB組件:

<template>
  <div>
    <h2>Hello,我是ChildB組件</h2>
  </div>
</template>

<script>
export default {

}
</script>

運(yùn)行效果如下


image.png

1.父組件傳遞子組件,子組件接收父組件

這里我想
parents組件給ChildA組件傳一個(gè)Hi , ChildA! 我是你爸爸! ,
parents組件給ChildB組件傳一個(gè)Hi , ChildB! 我也是你爸爸!
那么該怎么處理?這里子組件在父組件中作為標(biāo)簽引入,通過設(shè)置標(biāo)簽的屬性傳遞數(shù)據(jù),在子組件用props接收
注這里的props可接收類型

props: {
  title: String,
  likes: Number,
  isPublished: Boolean,
  commentIds: Array,
  author: Object,
  callback: Function,
  contactsPromise: Promise // or any other constructor
}

這里我們設(shè)置一個(gè)標(biāo)簽屬性message

parents組件:

<template>
  <div>
    <childA :message="toChildA"/>
    <childB :message="toChildB"/>
  </div>
</template>

<script>
export default {
  components: {
    childA: () => import('@/components/childA'),
    childB: () => import('@/components/childB'),
  },
  data(){
      return{
          toChildA:'Hi , ChildA! 我是你爸爸!',
          toChildB:'Hi , ChildB! 我也是你爸爸!',
      }
  }
}
</script>


childA組件:

<template>
  <div>
    <h2>Hello,我是ChildA組件</h2>
    <div>來自parents組件的信息:{{message}}</div>
  </div>
</template>

<script>
export default {
    props:{
        message:String
    }
}
</script>

childB組件:

<template>
  <div>
    <h2>Hello,我是ChildB組件</h2>
    <div>來自parents組件的信息:{{message}}</div>
  </div>
</template>

<script>
  export default {
    props:{
      message:String
    }
  }
</script>

運(yùn)行效果如下


image.png

2.子組件傳遞父組件,父組件接收子組件

子組件通過$emit觸發(fā)事件,父組件通過v-on接收觸發(fā)事件

通俗的來說 childA組件: $emit('A觸發(fā)的事件名稱') 那么相應(yīng)的 parents組件 就需要接收同樣的事件
<childA @A觸發(fā)的事件名稱="parents組件用于接收的方法" />

parents組件:

<template>
  <div>
    <childA @from_child_a="receiveChildA" :message="toChildA"/>
    <i>這是ChildA發(fā)過來的消息 : {{childAMsg}}</i>
    <childB @from_child_b="receiveChildB" :message="toChildB"/>
    <i>這是ChildB發(fā)過來的消息 : {{childBMsg}}</i>
  </div>
</template>

<script>
export default {
  components: {
    childA: () => import('@/components/childA'),
    childB: () => import('@/components/childB'),
  },
  data(){
      return{
        toChildA:'Hi , ChildA! 我是你爸爸!',
        toChildB:'Hi , ChildB! 我也是你爸爸!',
        childAMsg:'',
        childBMsg:''
      }
  },
  methods:{
    receiveChildA(pramas){
      this.childAMsg = pramas
    },
    receiveChildB(pramas){
      this.childBMsg = pramas
    }
  }
}
</script>

childA組件:

<template>
  <div>
    <h2>Hello,我是ChildA組件</h2>
    <div>來自parents組件的信息:{{message}}</div>
  </div>
</template>

<script>
export default {
    props:{
        message:String
    },
    mounted(){
      this.$emit('from_child_a','Hi,爸爸 , 我是ChildA')
    }
}
</script>

childB組件:

<template>
  <div>
    <h2>Hello,我是ChildB組件</h2>
    <div>來自parents組件的信息:{{message}}</div>
  </div>
</template>

<script>
  export default {
    props:{
      message:String
    },
    mounted(){
      this.$emit('from_child_b','Hi,爸爸 , 我是ChildB')
    }
  }
</script>

運(yùn)行效果如下


image.png

3.平行兄弟組件之間的數(shù)據(jù)傳遞

上面我們講了子傳父,父傳子,子接收父,父接收子,那么平行的兩個(gè)組件之間我們該怎么通信呢?這里引入一個(gè)概念‘總線’,你可以把它理解為全局的window,但是又不同于window,它的作用就好像十字路口,就是我不管你什么方向的車,你都可以從我這里過,而我既可以接受你的信號,我也能把你的信號傳遞給別人。

具體怎么實(shí)現(xiàn)的?

這里我們新建一個(gè)bus.js ,新建一個(gè)Vue對象并且掛載注入到vue對象里,在main.js引用

import Vue from 'vue'

export const Bus = new Vue()

export default Vue => {
  const bus = Bus;
  Vue.bus = bus;
  Vue.prototype.$bus = bus
}

main.js引入使用,這樣全局就注冊了一個(gè)bus總線
注意:注冊的總線事件要在組件銷毀時(shí)卸載,否則會多次掛載,造成觸發(fā)一次但多個(gè)響應(yīng)的情況

import Bus from './utils/bus'
Vue.use(Bus)

ChildA組件

<template>
  <div>
    <h2>Hello,我是ChildA組件</h2>
    <div>來自parents組件的信息:{{message}}</div>
    <button @click="sendMsgToB">我要給ChildB發(fā)送信息</button>
  </div>
</template>

<script>
export default {
    props:{
        message:String
    },
    mounted(){
      this.sendToParent()
    },
    methods:{
      sendToParent(){
        this.$emit('from_child_a','Hi,爸爸 , 我是ChildA');
      },
      sendMsgToB(){
        this.$bus.$emit('from_bro_child_a','Hi,兄弟,我是ChildA'+Math.random())
      }
    },
    beforeDestroy () {
      this.$bus.$off('from_bro_child_a')
    }
}
</script>

ChildB組件

<template>
  <div>
    <h2>Hello,我是ChildB組件</h2>
    <div>來自parents組件的信息:{{message}}</div>
    <div style="color: red">來自兄弟ChildA組件的信息:{{fromBroChildAMsg}}</div>
  </div>
</template>

<script>
  export default {
    props:{
      message:String
    },
    data(){
      return{
        fromBroChildAMsg:null
      }
    },
    mounted(){
      this.sendToParent()
      this.watchBus()
    },
    methods:{
      sendToParent(){
        this.$emit('from_child_b','Hi,爸爸 , 我是ChildB');
      },
      watchBus(){
        let that = this;
        this.$bus.$on('from_bro_child_a', (pramas) => {
          that.fromBroChildAMsg= pramas
        })
      }
    }
  }
</script>

運(yùn)行效果如下


image.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容