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

