Vue自定義事件(詳解)
前面的話(huà)
父組件使用props傳遞數(shù)據(jù)給子組件,子組件怎么跟父組件通信呢?這時(shí),Vue的自定義事件就派上用場(chǎng)了。本文將詳細(xì)介紹Vue自定義事件
事件綁定
每個(gè) Vue 實(shí)例都實(shí)現(xiàn)了事件接口 (Events interface),即
使用 $on(eventName) 監(jiān)聽(tīng)事件 使用 $emit(eventName) 觸發(fā)事件
[注意]Vue 的事件系統(tǒng)分離自瀏覽器的EventTarget API。盡管它們的運(yùn)行類(lèi)似,但是 $on 和 $emit 不是addEventListener 和 dispatchEvent 的別名
另外,父組件可以在使用子組件的地方直接用 v-on 來(lái)監(jiān)聽(tīng)子組件觸發(fā)的事件
[注意]不能用 $on 偵聽(tīng)子組件拋出的事件,而必須在模板里直接用 v-on 綁定
<div id="example"> <parent></parent> </div>
<script>
var childNode = {
template: `<button @click="incrementCounter">{{ counter }}</button>`,
data(){
return {
counter: 0
}
},
methods:{
incrementCounter(){
this.counter ++;
this.$emit('increment');
}
},
}
var parentNode = {
template: `
<div class="parent">
<p>{{total}}</p>
<child @increment="incrementTotal"></child>
<child @increment="incrementTotal"></child>
</div>
`,
components: {
'child': childNode
},
data(){
return {
'total':0
}
},
methods:{
incrementTotal(){
this.total ++;
}
}
};
// 創(chuàng)建根實(shí)例
new Vue({
el: '#example',
components: {
'parent': parentNode
}
})
</script>
命名約定
自定義事件的命名約定與組件注冊(cè)及props的命名約定都不相同,由于自定義事件實(shí)質(zhì)上也是屬于HTML的屬性,所以其在HTML模板中,最好使用中劃線(xiàn)形式
<child @pass-data="getData"></child>
而子組件中觸發(fā)事件時(shí),同樣使用中劃線(xiàn)形式
this.$emit('pass-data',this.childMsg)
數(shù)據(jù)傳遞
子組件通過(guò)$emit可以觸發(fā)事件,第一個(gè)參數(shù)為要觸發(fā)的事件,第二個(gè)事件為要傳遞的數(shù)據(jù)
this.$emit('pass-data',this.childMsg)
父組件通過(guò)$on監(jiān)聽(tīng)事件,事件處理函數(shù)的參數(shù)則為接收的數(shù)據(jù)
getData(value){
this.msg = value;
}
<div id="example"> <parent></parent> </div>
<script>
var childNode = {
template: `
<div class="child">
<div>
<span>子組件數(shù)據(jù)</span>
<input v-model="childMsg" @input="data">
</div>
<p>{{childMsg}}</p>
</div>
`,
data(){
return{
childMsg:''
}
},
methods:{
data(){
this.$emit('pass-data',this.childMsg)
}
}
}
var parentNode = {
template: `
<div class="parent">
<div>
<span>父組件數(shù)據(jù)</span>
<input v-model="msg">
</div>
<p>{{msg}}</p>
<child @pass-data="getData"></child>
</div>
`,
components: {
'child': childNode
},
data(){
return {
'msg':'match'
}
},
methods:{
getData(value){
this.msg = value;
}
}
};
// 創(chuàng)建根實(shí)例
new Vue({
el: '#example',
components: {
'parent': parentNode
}
})
</script>
sync修飾符
在一些情況下,可能會(huì)需要對(duì)一個(gè) prop 進(jìn)行雙向綁定。事實(shí)上,這正是Vue1.x中的 .sync修飾符所提供的功能。當(dāng)一個(gè)子組件改變了一個(gè) prop 的值時(shí),這個(gè)變化也會(huì)同步到父組件中所綁定的值。這很方便,但也會(huì)導(dǎo)致問(wèn)題,因?yàn)樗茐牧藛蜗驍?shù)據(jù)流的假設(shè)。由于子組件改變 prop 的代碼和普通的狀態(tài)改動(dòng)代碼毫無(wú)區(qū)別,當(dāng)光看子組件的代碼時(shí),完全不知道它何時(shí)悄悄地改變了父組件的狀態(tài)。這在 debug 復(fù)雜結(jié)構(gòu)的應(yīng)用時(shí)會(huì)帶來(lái)很高的維護(hù)成本,上面所說(shuō)的正是在 2.0 中移除 .sync 的理由
從 2.3.0 起重新引入了 .sync 修飾符,但是這次它只是作為一個(gè)編譯時(shí)的語(yǔ)法糖存在。它會(huì)被擴(kuò)展為一個(gè)自動(dòng)更新父組件屬性的 v-on 偵聽(tīng)器
<comp :foo.sync="bar"></comp>
會(huì)被擴(kuò)展為:
<comp :foo="bar" @update:foo="val => bar = val"></comp>
當(dāng)子組件需要更新 foo 的值時(shí),它需要顯式地觸發(fā)一個(gè)更新事件:
this.$emit('update:foo', newValue)
因此,可以使用.sync來(lái)簡(jiǎn)化自定義事件的操作,實(shí)現(xiàn)子組件向父組件的數(shù)據(jù)傳遞
<div id="example">
<parent></parent>
</div>
<script src="https://unpkg.com/vue"></script>
<script>
var childNode = {
template: `
<div class="child">
<div>子組件數(shù)據(jù):{{childMsg}}</div>
<input v-model="childMsg">
<button @click=add >+1</button>
</div>
`,
data(){
return{
childMsg: 0
}
},
methods:{
add(){
this.childMsg++;
this.$emit('update:foo',this.childMsg);
}
}
};
var parentNode = {
template: `
<div class="parent">
<p>父組件數(shù)據(jù):{{msg}}</p>
<child :foo.sync="msg"></child>
</div>
`,
components: {
'child': childNode
},
data(){
return {
'msg':0
}
}
};
// 創(chuàng)建根實(shí)例
new Vue({
el: '#example',
components: {
'parent': parentNode
}
})
</script>
以上這篇Vue自定義事件(詳解)就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue3(optionApi)使用Element Plus庫(kù)沒(méi)有效果的解決方式
這篇文章主要介紹了vue3(optionApi)使用Element Plus庫(kù)沒(méi)有效果的解決方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03
安裝vue-cli報(bào)錯(cuò) -4058 的解決方法
這篇文章主要介紹了安裝vue-cli報(bào)錯(cuò) -4058 的解決方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-10-10
vue2.0+SVG實(shí)現(xiàn)音樂(lè)播放圓形進(jìn)度條組件
這篇文章主要為大家詳細(xì)介紹了Vue2.0+SVG實(shí)現(xiàn)音樂(lè)播放圓形進(jìn)度條組件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-09-09
Vue3實(shí)現(xiàn)粒子動(dòng)態(tài)背景的示例詳解
這篇文章主要為大家詳細(xì)介紹了如何利用Vue3實(shí)現(xiàn)粒子動(dòng)態(tài)背景,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,感興趣的小伙伴可以跟隨小編一起了解一下2023-11-11
elementui源碼學(xué)習(xí)之仿寫(xiě)一個(gè)el-divider組件
這篇文章主要為大家介紹了elementui源碼學(xué)習(xí)之仿寫(xiě)一個(gè)el-divider組件示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08
使用 webpack 插件自動(dòng)生成 vue 路由文件的方法
這篇文章主要介紹了使用 webpack 插件自動(dòng)生成 vue 路由文件的方法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-08-08
vue中使用webuploader做斷點(diǎn)續(xù)傳實(shí)現(xiàn)文件上傳功能
之前做的一個(gè)項(xiàng)目中,由于經(jīng)常上傳幾百兆的壓縮包,導(dǎo)致經(jīng)常上傳失敗,所以就找了webuploader插件做了斷點(diǎn)續(xù)傳,斷點(diǎn)續(xù)傳除了需要前端分片,也需要后臺(tái)去支持,所以做的時(shí)候做好對(duì)接協(xié)調(diào),所以本文就給大家詳細(xì)的介紹一下vue中如何使用webuploader做斷點(diǎn)續(xù)傳2023-07-07

