vue中$refs, $emit, $on, $once, $off的使用詳解
1.$refs的使用場(chǎng)景
父組件調(diào)用子組件的方法,可以傳遞數(shù)據(jù)。
父組件:
<div id="app">
<child-a ref="child"></child-a>
<button @click="getMyEvent">點(diǎn)擊父組件</button>
<div>
<script>
import ChildA from './child.vue'
export default{
components:{
ChildA
},
data(){
return {
msg:'我是父組件的數(shù)據(jù)'
}
},
methods:{
getMyEvent(){
//調(diào)用子組件的方法,child是上邊ref起的名字,emitEvent是子組件的方法。
this.$refs.child.emitEvent(this.msg)
}
}
}
</script>
子組件:
<template>
<button>點(diǎn)擊我</button>
</template>
<script>
export default{
methods:{
emitEvent(msg){
console.log('接收的數(shù)據(jù)------'+msg)
}
}
}
</script>
2.$emit的使用
子組件調(diào)用父組件的方法并傳遞數(shù)據(jù)。
子組件:
<template>
<button @click="emitEvent">點(diǎn)擊我</button>
</template>
<script>
export default{
data(){
return{
msg:'我是子組件的數(shù)據(jù)'
}
},
methods:{
emitEvent(){
//通過(guò)按鈕的點(diǎn)擊事件觸發(fā)方法,然后用$emit觸發(fā)一個(gè)my-event的自定義方法,傳遞this.msg數(shù)據(jù)。
this.$emit('my-event',this.msg)
}
}
}
</script>
父組件:
<template>
<div id="app">
<child-a @my-event="getMyEvent"></child-a>
//父組件通過(guò)監(jiān)測(cè)my-event事件執(zhí)行一個(gè)方法,然后取到子組件中傳遞過(guò)來(lái)的值。
</div>
</template>
<script>
import childA from './child.vue';
export default {
components:{
childA
},
methods:{
getMyEvent(msg){
console.log('接收數(shù)據(jù)---'+msg);
//接收數(shù)據(jù),我是子組件的數(shù)據(jù)
}
}
}
</script>
3.$on的使用場(chǎng)景
兄弟組件之間相互傳遞數(shù)據(jù)。
首先創(chuàng)建一個(gè)Vue的空白實(shí)例(兄弟組件的橋梁)
import Vue from 'vue'; export default new Vue();
子組件A:發(fā)送放使用$emit自定義事件把數(shù)據(jù)帶過(guò)去。
<template>
<div>
<span>A組件-{{msg}}</span>
<input type="button" value="把A組件數(shù)據(jù)傳遞給B" @click="send">
</div>
</template>
<script>
import eventBus from './eventBus';
export default{
data(){
return{
msg:{
a:'111',
b:'222'
}
}
},
methods:{
send(){
eventBus.$emit('aevent',this.msg)
}
}
}
</script>
子組件B:接收方通過(guò)$on監(jiān)聽(tīng)自定義事件的callback接收數(shù)據(jù)
<template>
<div>
<span>B組件,A傳的數(shù)據(jù)為--{{msg}}</span>
</div>
</template>
<script>
import eventBus from './eventBus.vue'
export default {
data(){
return{
msg:''
}
},
mounted(){
eventBus.$on('aevent',(val)=>{//監(jiān)聽(tīng)事件aevent,回調(diào)函數(shù)要使用箭頭函數(shù)。
console.log(val);//打印結(jié)果;我是a組件的數(shù)據(jù)。
})
}
}
</script>
父組件:
<template>
<div>
<childa></childa>
<br/>
<childb></childb>
</div>
</template>
<script>
import childa from './childa.vue';
import childb from './childb.vue';
export default{
componets:{
childa,
childb
},
data(){
return{
msg:''
}
}
}
</script>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
解決vue路由后界面沒(méi)有變化,但是鏈接有的問(wèn)題
今天小編就為大家分享一篇解決vue路由后界面沒(méi)有變化,但是鏈接有的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-09-09
springboot+vue+對(duì)接支付寶接口+二維碼掃描支付功能(沙箱環(huán)境)
這篇文章主要介紹了springboot+vue+對(duì)接支付寶接口+二維碼掃描支付(沙箱環(huán)境),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-10-10
LRU算法在Vue內(nèi)置組件keep-alive中的使用
LRU算法全稱(chēng)為least recently use 最近最少使用,核心思路是最近被訪問(wèn)的以后被訪問(wèn)的概率會(huì)變高,那么可以把之前沒(méi)被訪問(wèn)的進(jìn)行刪除,維持一個(gè)穩(wěn)定的最大容量值,從而不會(huì)導(dǎo)致內(nèi)存溢出。2021-05-05
vuejs2.0子組件改變父組件的數(shù)據(jù)實(shí)例
本篇文章主要介紹了vuejs2.0子組件改變父組件的數(shù)據(jù)實(shí)例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-05-05
解決Vue中引入swiper,在數(shù)據(jù)渲染的時(shí)候,發(fā)生不滑動(dòng)的問(wèn)題
今天小編就為大家分享一篇解決Vue中引入swiper,在數(shù)據(jù)渲染的時(shí)候,發(fā)生不滑動(dòng)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-09-09
vue如何搭建多頁(yè)面多系統(tǒng)應(yīng)用
這篇文章主要為大家詳細(xì)介紹了vue搭建多頁(yè)面多系統(tǒng)應(yīng)用的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-06-06
vue語(yǔ)法自動(dòng)轉(zhuǎn)typescript(解放雙手)
這篇文章主要介紹了vue語(yǔ)法自動(dòng)轉(zhuǎn)typescript,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09

