vue中的事件觸發(fā)(emit)及監(jiān)聽(on)問題
vue事件觸發(fā)(emit)及監(jiān)聽(on)
每個 vue 實例都實現(xiàn)了事件接口
- 1.使用 $on(eventName,callback) 監(jiān)聽事件
- 2.使用 $emit(eventName,[…args]) 觸發(fā)事件
$emit 和 $on 必須都在實例上進行觸發(fā)和監(jiān)聽。
// on監(jiān)聽emit觸發(fā)的事件
created:function(){
? ? this.$on('emitFn',(arg)=> {
? ? ? ? ? console.log('on監(jiān)聽參數(shù)====',arg) ?//['string',false,{name:'vue'}]
? ? ? })
? },
? methods:{
? ? emit () {
? ? ?? ?// $emit 事件觸發(fā) ?參數(shù)是多個不同的數(shù)據(jù)類型時 用數(shù)組傳遞
? ? ? ? ?this.$emit('emitFn',['string',false,{name:'vue'}])
? ? ? ? ?
? ? ? ? ?// 監(jiān)聽多個emit事件,將事件名用數(shù)組形式寫 ?['emitFn1','emitFn2'];
? ? ? ? ? this.$emit(['emitFn1','emitFn2'],'arg1')
? ? ? }
? }案例
通過在父級組件中,拿到子組件的實例進行派發(fā)事件,然而在子組件中事先進行好派好事件監(jiān)聽的準備,接收到一一對應的事件進行一個回調(diào),同樣也可以稱之為封裝組件向父組件暴露的接口。
vue emit事件無法觸發(fā)問題
在父組件中定義事件監(jiān)聽,會出現(xiàn)無法觸發(fā)對應的事件函數(shù),在下面的代碼中,我想通過v-on:event_1=“handle”, 想監(jiān)聽子組件中的event_1事件,但盡管事件發(fā)生了, 但還是觸發(fā)不了,這個問題在于v-on:event_1="handle"的位置,需要放在 <my-template :users=“users” v-on:event_1=“handle” ></my-template> 中。
<body>
<div id='app' v-on:event_1="handle1">
<my-template :users="users"></my-template>
</div>
</body>
<script>
Vue.component('my-template', {
data: function () {
return {
test:"hello"
}
},
props: ["users"],
template: `
<div>
<ul>
<li v-for="item in users" :key="item.id">
<div>
<label>name:</label>
{{item.name}}
<label>content:</label>
{{item.content}}
<label>time:</label>
{{item.time}}
<input type="button" value="remove" @click="remove(item.id)"></input>
<input type="button" value="通知" @click="$emit('event_1',this)"></input>
</div>
</li>
</ul>
</div>
`,
methods:{
remove(id){
console.log(id);
for(let i = 0; i<this.users.length;++i){
if(this.users[i].id == id){
this.users.splice(i,1);
break;
}
}
},
notice(id){
console.log("notice", id);
},
handle(e){
console.log("son handle",e)
}
}
})
var vm = new Vue({
el: '#app',
data: {
posts: [
{ id: 1, title: 'My journey with Vue' },
{ id: 2, title: 'Blogging with Vue' },
{ id: 3, title: 'Why Vue is so fun' }
],
postFontSize: 1,
searchText: 'hello',
users:[
{
name:"zhangsan",
id:'1',
time:new Date().getUTCDate(),
content:"白日依山盡,黃河入海流"
},
{
name:"lisi",
id:'2',
time:new Date().getUTCDate(),
content:"會當凌絕頂,一覽眾山小"
},
{
name:"wangwu",
id:'3',
time:new Date().getUTCDate(),
content:"大漠孤煙直,長河落日圓"
}
]
},
methods:{
handle1(e){
console.log("event 事件觸發(fā),參數(shù)為:",e);
}
}
})
</script>
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
- Vue 組件事件觸發(fā)和監(jiān)聽實現(xiàn)源碼解析
- Vue?click事件傳遞參數(shù)的示例教程
- vue中@click綁定事件點擊不生效的原因及解決方案
- Vue中使用element-ui給按鈕綁定一個單擊事件實現(xiàn)點擊按鈕就彈出dialog對話框
- vue中如何給el-table-column添加指定列的點擊事件
- vue長按事件和點擊事件沖突的解決
- vue項目如何實現(xiàn)Echarts在label中獲取點擊事件
- Vue Element-ui 鍵盤事件失效的解決
- Vue如何給組件添加點擊事件?@click.native
- vue中可以綁定多個事件嗎
- vant/vue手機端長按事件以及禁止長按彈出菜單實現(xiàn)方法詳解
相關文章
vue-treeselect無法點擊問題(點擊無法出現(xiàn)拉下菜單)
這篇文章主要介紹了vue-treeselect無法點擊問題(點擊無法出現(xiàn)拉下菜單),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-04-04
Vue瀏覽器鏈接與接口參數(shù)實現(xiàn)加密過程詳解
這篇文章主要介紹了Vue瀏覽器鏈接與接口參數(shù)實現(xiàn)加密過程,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習吧2022-12-12
vue項目實現(xiàn)背景顏色以及下劃線從左到右漸變動畫效果
這篇文章主要介紹了vue項目實現(xiàn)背景顏色以及下劃線從左到右漸變動畫效果,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-08-08
在vue中獲取微信支付code及code被占用問題的解決方法
這篇文章主要介紹了在vue中獲取微信支付code及code被占用問題的解決方法。小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-04-04

