Skip to content

小程序的EventBus,跨页通信解决方案.微信、支付宝小程序

Notifications You must be signed in to change notification settings

manbinzheng/event-bus

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

具体调用方法

App 是小程序的实例,在每个 Page 里都能通过执行 getApp 函数获取到它。我们可以把 Event 类的实例挂载在 App 中,方便每个 Page 去调用。

// app.js

const Event = require('./libs/event')

App({
    event: new Event(),
    ...
})

订单列表页在 onLoad 生命周期中订阅 “afterPaySuccess” 事件。

//order_list.js

var App = getApp()

Page({
    onLoad: function(){
        App.event.on('afterPaySuccess',this.afterPaySuccess, this)
    },
    afterPaySuccess: function(orderId) {
        // do sth
    },
})

在订单详情页支付成功的回调中,发布 “afterPaySuccess” 事件,同时带上订单 id 参数。

//order_detail.js

var App = getApp()

Page({
    raisePayment: function() {
        App.event.emit('afterPaySuccess', orderId)
    },
})

所有 Page 的 onUnload 生命周期,必须注销掉之前订阅的事件。注销方法 off 的调用姿势有三种,不过还是建议注销当前 Page 所订阅的事件,而不是注销所有的。 tip: 在那里订阅就在注销

var App = getApp()

Page({
    onLoad: function(){
        App.event.on('afterPaySuccess',this.afterPaySuccess, this)
    },
    afterPaySuccess: function(orderId) {
        // do sth
    },
    onUnload: function(){
        // remove all
        App.event.off()
        // remove all callbacks
        App.event.off('afterPaySuccess')
        // remove specific callbacks
        App.event.off('afterPaySuccess', this.afterPaySuccess)
    }
})

About

小程序的EventBus,跨页通信解决方案.微信、支付宝小程序

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published