vue框架下組件間通信的方式有多少,操作是什么
Admin 2022-06-30 群英技術(shù)資訊 998 次瀏覽
關(guān)于“vue框架下組件間通信的方式有多少,操作是什么”的知識有一些人不是很理解,對此小編給大家總結(jié)了相關(guān)內(nèi)容,具有一定的參考借鑒價值,而且易于學(xué)習(xí)與理解,希望能對大家有所幫助,有這個方面學(xué)習(xí)需要的朋友就繼續(xù)往下看吧。一個組件里面引入另外一個組件,此時構(gòu)成了一種“父子關(guān)系”,當(dāng)前組件為“父”,引入的組件為“子”,如當(dāng)前組件(父),在父組件中通過 “:message” 向子組件通信。
<template>
<div class="parent-box">
<div>
<div>我是父頁面</div>
<div>{{message}}</div>
</div>
<children :message="toChildrenMsg"></children>
</div>
</template>
<script>
import Children from './Children.vue' //當(dāng)前頁引入子組件
export default {
name:"Parent",
components:{
Children
},
data(){
return {
message:'我是父頁面的內(nèi)容',
toChildrenMsg:'從父頁面?zhèn)鬟^到子頁面的內(nèi)容'
}
}
}
</script>
在子組件通過props進(jìn)行接收,注意子組件props里面接收的對象名稱必須與父組件中在子組件綁定的名稱一致,當(dāng)前例子為“message”,可以在組件return中this.的方式使用props里面的值。
<template>
<div class="children-box">
<div>
<div>我是子頁面</div>
<div>{{message}}</div>
</div>
</div>
</template>
<script>
export default {
name:"Children",
props:{
message:{
type:String, //類型判斷
default:'' //默認(rèn)值
}
}
}
</script>
子組件接收到父組件傳過來的內(nèi)容,實現(xiàn)效果如下圖所示:

在子組件中通過this.$emit()方法向父組件通信,如下,點擊觸發(fā)事件,執(zhí)行this.$emit('fromChildMethod'),觸發(fā)父組件的fromChildMethod方法。
<template>
<div class="children-box">
<div>
<div>我是子頁面</div>
<div>{{message}}</div>
<div><span @click="toParentMethod">點擊觸發(fā)父頁面事件</span></div>
</div>
</div>
</template>
<script>
export default {
name:"Children",
props:{
message:{
type:String,
default:''
}
},
methods:{
toParentMethod(){
this.$emit('fromChildMethod')
}
}
}
</script>
在父組件的子組件上綁定fromChildMethod方法,對該方法進(jìn)行監(jiān)聽,當(dāng)該方法觸發(fā)時,執(zhí)行父組件中相應(yīng)的方法fromChild。
<template>
<div class="parent-box">
<div>
<div>我是父頁面</div>
<div style="font-size:12px;">{{message}}</div>
<div style="font-size:12px;color:red">{{fromChildMsg}}</div>
</div>
<children :message="toChildrenMsg" @fromChildMethod="fromChild"></children>
</div>
</template>
<script>
import Children from './Children.vue'
export default {
name:"Parent",
components:{
Children
},
data(){
return {
message:'我是父頁面的內(nèi)容',
toChildrenMsg:'從父頁面?zhèn)鬟^到子頁面的內(nèi)容',
fromChildMsg:''
}
},
methods:{
fromChild(){
this.fromChildMsg = '子頁面觸發(fā)的方法' //監(jiān)聽到子組件觸發(fā)的方法,顯示該內(nèi)容
}
}
}
</script>
當(dāng)點擊子組件的對應(yīng)的span,觸發(fā)方法,向父組件進(jìn)行通知。


小結(jié):父傳子,props;子傳父,this.$emit();觸發(fā)、監(jiān)聽名稱須一致。
真實的場景中,組件不僅僅是“父子”關(guān)系,還有“兄弟”關(guān)系跟跨層級組件等等。這時候props跟$emit可能就不太適用了,這時候它出現(xiàn)了,那就是Bus(事件總線),父子組件同樣適用。
Bus之觸發(fā)$emit、監(jiān)聽$on、關(guān)閉$off,主要用到的就是$emit跟$on。
先在項目中新建一個文件夾bus,里面有個index.js文件,創(chuàng)建一個新的Vue實例,然后導(dǎo)出模塊。

接下來import這個新的Vue實例,也就是bus,常用的兩種導(dǎo)入方式,一種是全局導(dǎo)入,另外一種是局部導(dǎo)入(需每個組件都導(dǎo)入一次)。以下為全局導(dǎo)入,在main.js里面將該bus作為當(dāng)前Vue實例的原型方法,能直接在各組件里面通過this.bus的方式調(diào)用。
import Vue from 'vue'
import App from './App'
import bus from './bus/index'
Vue.prototype.bus = bus
new Vue({
el: '#app',
components: { App },
template: '<App/>'
})
下面展示實現(xiàn)bus通信過程,場景為父子,同樣的,兄弟、跨層級用法與其類似:
Parent組件中向Children組件通信,通過this.bus.$emit()觸發(fā)
<template>
<div class="parent-box">
<div>
<div>我是父頁面</div>
<div @click="toChildBus"><span>向子組件通信</span></div>
</div>
<children></children>
</div>
</template>
<script>
import Children from './Children.vue'
export default {
name:"Parent",
components:{
Children
},
methods:{
toChildBus(){
let val = '父組件向子組件通信'
this.bus.$emit('toChild',val) //val為傳過去的值,非必傳
}
}
}
</script>
Children組件監(jiān)聽Parent組件觸發(fā)的事件(在mounted階段進(jìn)行綁定監(jiān)聽),注意事件名稱要一致,通過this.bus.$on()監(jiān)聽,當(dāng)總線中監(jiān)聽到觸發(fā)該方法,拿到傳過來的值(也可以在里面執(zhí)行自定義方法)。
<template>
<div class="children-box">
<div>
<div>我是子頁面</div>
<div style="font-size:12px;color:blue;">{{fromParentMsg}}</div>
</div>
</div>
</template>
<script>
export default {
name:"Children",
data(){
return {
fromParentMsg:''
}
},
mounted(){
this.bus.$off('toChild')
this.bus.$on('toChild',val=>{
this.fromParentMsg = val //此處為復(fù)制操作,也可在里面執(zhí)行相應(yīng)的方法
})
}
}
</script>
效果圖:


總結(jié):父子,兄弟,跨級(祖孫等)通信寫法相同,就不一一舉例了,都是通過this.bus.$emit()觸發(fā),通過this.bus.$on()監(jiān)聽,執(zhí)行相應(yīng)的操作,切記:觸發(fā)、監(jiān)聽名稱必須相同!
Vuex相當(dāng)于一個倉庫,你可以往倉庫里面放一些東西,保持存進(jìn)去的時的狀態(tài),可以修改,也可以在需要的時候取出,是一個全局狀態(tài)。本次只講如何使用vuex進(jìn)行通信,不深究其原理。
安裝vuex
npm install vuex --save
這里我新建一個文件夾,名稱為store,里面有一個index.js文件,創(chuàng)建一個Vuex.Store實例,然后導(dǎo)出這個實例,從圖中可以明確看出store的大致結(jié)構(gòu)及其要素,具體不展開講,關(guān)于vuex的相關(guān)文章數(shù)不勝數(shù),可以自行去了解,這里主要講大致用法。

在mian.js全局引入,之后就可以直接使用了。
import Vue from 'vue'
import App from './App'
import router from './router'
import bus from './bus/index'
import store from './store/index'
Vue.config.productionTip = false
Vue.prototype.bus = bus
new Vue({
el: '#app',
router,
store,
components: { App },
template: '<App/>'
})
方式一,this.$store.state.xxx,直接對state進(jìn)行操作,在一個組件mounted階段將值存如store中,當(dāng)然也可在你想在的方法中進(jìn)行操作。
<template>
<div class="parent-box">
<div>
<div>我是父頁面</div>
</div>
<children></children>
</div>
</template>
<script>
import Children from './Children.vue'
export default {
name:"Parent",
components:{
Children
},
data(){
return {
fromChildMsg:''
}
}
mounted(){
this.$store.state.msg = '父組件存入' //在此處通過方式一存起來
}
}
</script>
其他組件從store中取出,當(dāng)然同樣也可以進(jìn)行修改。
<template>
<div class="children-box">
<div>
<div>我是子頁面</div>
<div @click="fromStore"><span>從store里面取</span></div>
<div>{{fromStoreMsg}}</div>
</div>
</div>
</template>
<script>
export default {
name:"Children",
data(){
return {
fromStoreMsg:''
}
},
methods:{
fromStore(){
this.fromStoreMsg = this.$store.state.msg
}
}
}
</script>
效果圖:


方式二,通過this.$store.getters.xxx、mapGetters進(jìn)行取出。
// store/index.js
getters:{
getMsg:state=>{
return state.msg
}
},
//組件中取
this.$store.getters.getMsg
//也可以用mapGetters的方式
import { mapGetters } from 'vuex'
computed: {
...mapGetters(['getMsg'])
},
對store存入數(shù)據(jù)該可以用mutations、actions(可異步)進(jìn)行存入,具體就不展開了,有興趣可以自己去深究。
可以通過動態(tài)路由、路由跳轉(zhuǎn)方式進(jìn)行傳值,如this.$router.push({path:'xxx',query:{value:'xxx'}}),在跳轉(zhuǎn)的時候順便傳值,通過this.$route.params.value和this.$route.query.value獲取到傳過來的參數(shù)。該方式有局限性,只能在相互跳轉(zhuǎn)的組件通信取值,且直接在跳轉(zhuǎn)之后的頁面進(jìn)行刷新取不到值,視情況而用。
sessionStorage、localStorage、cookie
多個組件之間的通信除了可以用bus、store之外,還比較一種常用的方式--緩存,在同一個窗口不關(guān)閉的情況下,該窗口下的其他組件都可以取到緩存中已經(jīng)存好的值,利用sessionStorage.setItem(key,value)、localStorage.setItem(key,value)等將值存起來,其他組件可以通過sessionStorage.getItem(key)、localStorage.getItem(key)等方式拿到,多個頁面共享緩存數(shù)據(jù),刷新頁面數(shù)據(jù)不會銷毀,可以用sessionStorage.removeItem(key)、localStorage.removeItem(key)的方式將緩存移除,可用場景還是比較多的。
總結(jié):大致介紹vue組件中幾種常用的通信、傳值方式,考慮不同的場景使用不同的方式,提高開發(fā)效率,減少bug的產(chǎn)生。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:[email protected]進(jìn)行舉報,并提供相關(guān)證據(jù),查實之后,將立刻刪除涉嫌侵權(quán)內(nèi)容。
猜你喜歡
這篇文要給大家介紹的是JavaScript原型鏈,文章主要介紹內(nèi)容有構(gòu)造函數(shù)、屬性Prototype、屬性Prototype、屬性__proto__、訪問原型上的方法等問題,需要的朋友可以參考一下文章的詳細(xì)內(nèi)容
監(jiān)聽屬性可以針對某個屬性進(jìn)行監(jiān)聽,當(dāng)監(jiān)聽的屬性的值發(fā)生了變化,則會執(zhí)行相應(yīng)的函數(shù),下面這篇文章主要給大家介紹了關(guān)于Vue監(jiān)聽屬性的相關(guān)資料,需要的朋友可以參考下
這篇文章主要給大家分享jQuery鏈?zhǔn)秸{(diào)用的內(nèi)容,下文有對jQuery鏈?zhǔn)秸{(diào)用的介紹以及實例,對新手學(xué)習(xí)和理解jQuery鏈?zhǔn)秸{(diào)用有一定的幫助,感興趣的朋友就跟隨小編來一起學(xué)習(xí)吧。
javascript如何實現(xiàn)繼承?有哪些繼承方法?很多剛接觸javascrip繼承的朋友,可能對于javascrip繼承方法不是很清楚,因此這篇文章文章就給大家介紹一下JavaScript 實現(xiàn)繼承的幾種方式,小編認(rèn)為是比較實用的,下面就跟隨小編一起來了解一下吧。
vue怎樣實現(xiàn)修改密碼的功能?在vue中,我們想要做可以修改密碼的功能,可以通過兩個字段聯(lián)合校驗來實現(xiàn)。下文有實現(xiàn)代碼及詳細(xì)的代碼詳解,感興趣的朋友可以了解看看。
推薦內(nèi)容
相關(guān)標(biāo)簽
成為群英會員,開啟智能安全云計算之旅
立即注冊關(guān)注或聯(lián)系群英網(wǎng)絡(luò)
7x24小時售前:400-678-4567
7x24小時售后:0668-2555666
24小時QQ客服
群英微信公眾號
CNNIC域名投訴舉報處理平臺
服務(wù)電話:010-58813000
服務(wù)郵箱:[email protected]
投訴與建議:0668-2555555
Copyright ? QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版權(quán)所有
增值電信經(jīng)營許可證 : B1.B2-20140078 ICP核準(zhǔn)(ICP備案)粵ICP備09006778號 域名注冊商資質(zhì) 粵 D3.1-20240008