时间:2019-04-29 来源:小程序工厂
麻烦提供出现问题的具体机型、微信版本号、系统版本号,以及能复现问题的代码片段(https://developers.weixin.qq.com/miniprogram/dev/devtools/minicode.html)
event.js
//event.js
var events = {};
function on(name, that, callback) {
console.log('event->on', name)
var tuple = [that, callback];
var callbacks = events[name];
if (Array.isArray(callbacks)) {
callbacks.push(tuple);
} else {
events[name] = [tuple];
}
}
function remove(name, that) {
console.log('event->remove', name)
var callbacks = events[name];
if (Array.isArray(callbacks)) {
events[name] = callbacks.filter((tuple) => {
return tuple[0] != that;
})
}
}
function emit(name, data) {
console.log('event->emit', name, data)
var callbacks = events[name];
if (Array.isArray(callbacks)) {
callbacks.map((tuple) => {
var that = tuple[0];
var callback = tuple[1];
console.log('event ' + name + ': +1')
callback.call(that, data);
})
}
}
exports.on = on;
exports.remove = remove;
exports.emit = emit;
app.js中引入event.js
const event = require('./utils/event.js')
onLaunch: function(e) {
this.event = event
}
在首页在onLoad里监听:
getApp().event.on('checkToken', this, function(res) {
//根据res逻辑处理
})
在绑定页面完成操作后提交事件:
getApp().event.emit('checkToken', data)