FlashEvent
FlashEvent 小程序頁面間的通信工具 - 類似于EventBus,? 極易集成、使用
FlashEvent 在小程序中 能夠簡化各頁面間的通信,讓代碼書寫變得簡單,能有效的解耦事件發(fā)送方和接收方,能避免復(fù)雜和容易出錯(cuò)的依賴性和生命周期問題。
github url:?https://github.com/wuyajun7/FlashEvent
使用方式:
前置:將FlashEvent.js導(dǎo)入到項(xiàng)目的utils文件中
1、接收方j(luò)s代碼中
1.1 引入該類,如:let flashEvent = require('你的路徑/utils/FlashEvent.js');
1.2 注冊(cè)FlashEvent,如:在onLoad中
flashEvent.register(flashEvent.EVENT_KEYS.FIRST_EVENT, this, function (data) {
this.setData({ eventCallBack: data })
})
1.3 注銷FlashEvent,如:在onUnload中調(diào)用 flashEvent.unregister(flashEvent.EVENT_KEYS.FIRST_EVENT, this);
2、發(fā)送方j(luò)s代碼中
2.1 引入該類,如:let flashEvent = require('你的路徑/utils/FlashEvent.js');
2.2 發(fā)送事件,如:flashEvent.post(flashEvent.EVENT_KEYS.FIRST_EVENT, '發(fā)送的數(shù)據(jù)');