仿h5+fire自定義事件觸發監聽
uni-app調用
event.js
源碼記錄(點選檢視)
1.js下載下傳位址
[event.js](https://ext.dcloud.net.cn/plugin?id=333)
[源碼記錄](https://github.com/wojiaoxiaomayun/jsUtil/tree/master/event)
2.js檔案直接拖入
3.import event from 'common/event.js'
4.Vue.prototype.event = event;
接下來就可以使用了
使用執行個體
1.事件監聽
//事件監聽
//params:page:頁面的位址(确報每個頁面唯一)
// type: 事件類型
// callback: 回調參數
this.event.on('index/index','test',function(args){
//args為trigger中所有的參數,可自定義資料,除了type和page及success
console.log('testindex:' + args);
//傳回資料,在trigger中success方法可以收到
return {
};
});
//結果:testindex:index
2.事件觸發
//事件觸發
//params:obj:{type:'test',page:'index/index'}
// type:事件類型,對應監聽中的type,必須
// page: 預設all,表示所有頁面, 對應監聽中的page ,非必須
this.event.trigger({
type:'test',
page:'index/index'
//obj和test是舉的例子,随意啥都行,這個傳過去在on中的args中都可以擷取到
obj:{
},
test:{
},
success:function(data){
//data為on中傳回的資料
}
});
3.移除事件
this.event.remove('index/index')//移除所有
this.event.remove('index/index','test')//移除單個