npm i weapp-event -S
把实例挂载在 App 中,方便每个 Page 去调用
// app.js
import Event from 'weapp-event'
App({
event: Event,
...
})
//page.js
var app = getApp()
Page({
onLoad: function(){
app.event.on('do-foo', this, this.doFoo)
},
doFoo: function(id) {
...
},
...
})
//order_detail.js
var app = getApp()
Page({
doSomething: function() {
...
app.event.emit('do-foo', 123)
},
...
})
var app = getApp()
Page({
onUnload: function(){
// remove all
app.event.off()
// remove all callbacks
app.event.off('do-foo')
// remove specific callbacks
app.event.off('do-foo', this)
},
...
})