欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Vue如何實(shí)現(xiàn)組件間通信

 更新時(shí)間:2021年05月15日 14:13:20   作者:lovin  
組件間通信簡單來說就是組件間進(jìn)行數(shù)據(jù)傳遞。就像我們?nèi)粘5拇螂娫挘褪峭ㄓ嵉囊环N方式,你把話說給我聽,我把話說給你聽,說的話就是數(shù)據(jù)。電話就是通訊方式的一種。無論是 Vue 還是 React ,都得進(jìn)行組件間通信。

1. 父子間通信

最常見的就是父子之間的通信,通信是雙向的數(shù)據(jù)傳遞。

1.1 父組件 --> 兒子組件

父組件向兒子組件傳遞數(shù)據(jù)的方式就是 通過 Prop 向子組件傳遞數(shù)據(jù)。

//child.vue
<template>
    <div>
        我是兒子,我收到來自父親的數(shù)據(jù)為 {{value}}
    </div>
</template>

<script>
export default {
    props:{
        value: String
    }
}
//App.vue
<template>
  <div id="app">
    <Child :value="x" />
  </div>
</template>

<script>
import Child from './components/Child'
export default {
  data(){
    return {
      x: 'hi,child'
    }
  },
  components:{
    Child
  }
}
</script>

1.2 兒子組件 --> 父組件

兒子組件向父組件傳遞數(shù)據(jù)的方式就是通過子組件內(nèi) $emit 觸發(fā)自定義事件,子組件使用時(shí) v-on 綁定監(jiān)聽自定義事件。

這里的 v-on 事件通信是在子組件使用時(shí)作為子組件的事件屬性自動(dòng)進(jìn)行監(jiān)聽的。

因此兒子組件向父組件傳遞數(shù)據(jù),依賴于子組件使用時(shí)的自定義事件屬性。

//child.vue
<template>
    <div>
        我是兒子,我收到來自父親的數(shù)據(jù)為 {{value}}
        <button @click="sayHi">
            向父組件打招呼
        </button>
    </div>
</template>

<script>
export default {
    props:{
        value: String
    },
    methods:{
        sayHi(){
            this.$emit('sayHi','hi,parent!');
        }
    }
}
</script>
//App.vue
<template>
  <div id="app">
    我是父組件,我收到子組件傳來的數(shù)據(jù)為 {{y}}
    <Child :value="x" @sayHi="y = $event"/>
  </div>
</template>

<script>
import Child from './components/Child'
export default {
  data(){
    return {
      x: 'hi,child',
      y: ''
    }
  },
  components:{
    Child
  }
}
</script>

2. 爺孫間通信

爺孫間通信,可以使用兩次 v-on 通信,爺爺爸爸通信,然后爸爸兒子通信。

也可使用下方的任意組件間通信的方式。

3. 任意組件間通信

任意組件間通信就不再區(qū)分是 A 向 B 通信,還是 B 向 A 通信,而是通用的方式,誰想發(fā)送數(shù)據(jù)就使用對(duì)應(yīng)的 API 發(fā)送數(shù)據(jù),誰想要接收什么數(shù)據(jù),就使用對(duì)應(yīng)的 API 接收。

任意組件間通信有兩種方式,一種是使用 EventBus 發(fā)布訂閱模式通信,一種是使用 Vuex 通信。

3.1 EventBus

EventBus ,從字面意思理解就是事件公交車,所有觸發(fā)的事件傳遞的數(shù)據(jù)都從前門上車保存到公交車上,然后通過監(jiān)聽對(duì)應(yīng)事件提供的出口讓對(duì)應(yīng)的事件數(shù)據(jù)下車。

EventBus,實(shí)際意思是發(fā)布和訂閱模式,就是誰想把數(shù)據(jù)傳遞出去,就要通過觸發(fā)自定義事件的 API 進(jìn)行數(shù)據(jù)的發(fā)布;誰需要接收該數(shù)據(jù)信息的,就通過事件監(jiān)聽的 API 進(jìn)行數(shù)據(jù)的監(jiān)聽,一旦檢測到監(jiān)聽的數(shù)據(jù)發(fā)布出來,就會(huì)接收,這就是數(shù)據(jù)的訂閱。

EventBus 通信方式最重要是搞明白發(fā)布和訂閱的接口 API,在 Vue 中,Vue 實(shí)例有提供兩個(gè)接口,即 $emit$on ,因此可以新創(chuàng)建一個(gè)空的 Vue 實(shí)例,來獲得這兩個(gè)接口。

const eventBus = new Vue();
eventBus.$emit(eventName, […args]) //發(fā)布事件
eventBus.$on(event, callback)      //訂閱事件

實(shí)例如下:

// eventBus.js
import Vue from 'vue'
export const eventBus = new Vue();
//child
<template>
    <div>
        我是兒子,我收到來自父親的數(shù)據(jù)為 <strong>{{value}}</strong>
        <button @click="sayHi">
            向父組件打招呼
        </button>
        <button @click="sibling">
            向兄弟組件打招呼
        </button>
    </div>
</template>

<script>
import {eventBus} from '../eventBus.js'
export default {
    props:{
        value: String
    },
    methods:{
        sayHi(){
            this.$emit('sayHi','hi,parent!');
        },
        sibling(){
            eventBus.$emit('sibling','hi,brother');
        }
    }
}
</script>

<style scoped>
    strong{
        color: red;
    }
</style>
//sibling
<template>
    <div>
        我是兄弟組件,我收到來自兒子組件的數(shù)據(jù)信息為 <strong>{{x}}</strong> 
    </div>
</template>

<script>
import {eventBus} from '../eventBus.js'
export default {
    data(){
        return {
            x: ''
        }
    },
    mounted(){
        eventBus.$on('sibling', (msg)=>{
            this.x = msg;
        })
    }
}
</script>

<style scoped>
    strong{
            color: green;
    }
</style>
//parent
<template>
  <div id="app">
    我是父組件,我收到子組件傳來的數(shù)據(jù)為 <strong>{{y}}</strong>
    <Child :value="x" @sayHi="y = $event"/>
    <Sibling></Sibling>
  </div>
</template>

<script>
import Child from './components/Child'
import Sibling from './components/Sibling'
export default {
  data(){
    return {
      x: 'hi,child',
      y: ''
    }
  },
  components:{
    Child,
    Sibling
  }
}
</script>

<style scoped>
    strong{
            color: blue;
    }
</style>

關(guān)于 EventBus 這部分,可能存在這樣一個(gè)疑問,既然 Vue 實(shí)例中都有 $emit 和 $on,為什么不直接用 this.$emit 觸發(fā)事件, this.$on 接收事件呢?還非得要額外一個(gè)空實(shí)例 eventBus = new Vue() 。那是因?yàn)?,Vue 中每個(gè)組件都是一個(gè)單獨(dú)的 Vue 實(shí)例,你在這個(gè) Vue 實(shí)例中觸發(fā)該實(shí)例的 emit 事件,另外一個(gè)實(shí)例的 on 事件是接收不到的,不在一輛公交車上,怎么能進(jìn)行事件通信呢?因此就必須要一個(gè)公共的公交車,也就是事件總線。

上述實(shí)例中的 eventBus 的使用方法是局部的 eventBus,誰要用到 eventBus 要自己手動(dòng)引入。也可以將 eventBus 做成全局的,比如掛在 vue 的原型上。

//main.js
import Vue from 'vue'
import App from './App.vue'

Vue.config.productionTip = false

Vue.prototype.$eventBus = new Vue();//添加這句,一定要在下方的 new Vue 前。

new Vue({
  render: h => h(App),
}).$mount('#app')
//child
sibling(){
    this.$eventBus.$emit('sibling','hi,brother');
}
//sibling
mounted(){
    this.$eventBus.$on('sibling', (msg)=>{
        this.x = msg;
    })
}

除了上述的添加屬性到 Vue 原型的方式外,還可以使用 Object.defineProperty() 為 Vue 原型添加屬性。

import Vue from 'vue'
import App from './App.vue'

Vue.config.productionTip = false

let eventBus = new Vue()
Object.defineProperty(Vue.prototype,'$eventBus',{
  get(){
    return eventBus
  }
})

new Vue({
  render: h => h(App),
}).$mount('#app')

3.2 Vuex

Vue 組件間的通信也可使用專門為 vue.js 應(yīng)用程序開發(fā)的狀態(tài)管理模式:Vuex。Vuex 的使用比較復(fù)雜,詳細(xì)可見 Vuex 博客。Vuex 適用于大型的復(fù)雜的 Vue 項(xiàng)目的狀態(tài)管理。對(duì)于一些中小型的應(yīng)用程序,可以根據(jù) Vuex 的原理自定義 store 模式進(jìn)行狀態(tài)管理,vue 自定義狀態(tài)管理,可詳見 Vue 簡單狀態(tài)管理—store模式 博客。

無論是 Vuex 還是 自定義 store模式 ,其實(shí)現(xiàn)組件間通信的原理都是通過共享數(shù)據(jù)的方式實(shí)現(xiàn)的。組件間使用相同的數(shù)據(jù)源,當(dāng)一個(gè)組件改變數(shù)據(jù)時(shí),另一個(gè)組件依賴的數(shù)據(jù)源也就改變了。

以上就是Vue如何實(shí)現(xiàn)組件間通信的詳細(xì)內(nèi)容,更多關(guān)于Vue組件間通信的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Element-UI中Upload上傳文件前端緩存處理示例

    Element-UI中Upload上傳文件前端緩存處理示例

    這篇文章主要介紹了Element-UI中Upload上傳文件前端緩存處理示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2019-02-02
  • Vue2面試考點(diǎn)之$nextTick原理解析

    Vue2面試考點(diǎn)之$nextTick原理解析

    平時(shí)在獲取真實(shí)DOM的時(shí)候獲取不到最新的DOM元素,使用$nextTick就可以,那為什么$nextTick就可以獲取到最新的DOM元素呢,本文就來帶著這兩個(gè)問題來解析一下nextTick的原理
    2023-05-05
  • vue點(diǎn)擊按鈕動(dòng)態(tài)創(chuàng)建與刪除組件功能

    vue點(diǎn)擊按鈕動(dòng)態(tài)創(chuàng)建與刪除組件功能

    這篇文章主要介紹了vue點(diǎn)擊按鈕動(dòng)態(tài)創(chuàng)建與刪除組件功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-12-12
  • vue3?中?computed?新用法示例小結(jié)

    vue3?中?computed?新用法示例小結(jié)

    這篇文章主要介紹?vue3?中?computed?的新用法,對(duì)比?vue2?中的寫法,讓您快速掌握?vue3?中?computed?的新用法,對(duì)函數(shù)式寫法,options?寫法相關(guān)知識(shí)感興趣的朋友一起看看吧
    2021-11-11
  • Vue下滾動(dòng)到頁面底部無限加載數(shù)據(jù)的示例代碼

    Vue下滾動(dòng)到頁面底部無限加載數(shù)據(jù)的示例代碼

    本篇文章主要介紹了Vue下滾動(dòng)到頁面底部無限加載數(shù)據(jù)的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-04-04
  • vue 2.0組件與v-model詳解

    vue 2.0組件與v-model詳解

    這篇文章主要介紹了vue 2.0組件與v-model的相關(guān)資料,文中介紹的非常詳細(xì),本文適合初學(xué)組件編寫的同學(xué)閱讀,需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-03-03
  • 如何修改vant的less樣式變量

    如何修改vant的less樣式變量

    這篇文章主要介紹了如何修改vant的less樣式變量方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-07-07
  • Element InputNumber 計(jì)數(shù)器的實(shí)現(xiàn)示例

    Element InputNumber 計(jì)數(shù)器的實(shí)現(xiàn)示例

    這篇文章主要介紹了Element InputNumber 計(jì)數(shù)器的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-08-08
  • vue實(shí)現(xiàn)拖拽窗口功能

    vue實(shí)現(xiàn)拖拽窗口功能

    這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)拖拽窗口功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-04-04
  • vue中混入mixins的使用方法

    vue中混入mixins的使用方法

    mixins(混入)官方的描述是一種分發(fā) Vue 組件中可復(fù)用功能的非常靈活的方式,這篇文章主要給大家介紹了關(guān)于vue中混入 mixins使用的相關(guān)資料,需要的朋友可以參考下
    2021-10-10

最新評(píng)論