还可以使用mitt.js通过消息订阅的方式来实现。
在util新建文件mitt.js
function mitt(all) {
all = all || Object.create(null);
return {
on(type, handler) {
(all[type] || (all[type] = [])).push(handler);
},
off(type, handler) {
if (all[type]) {
all[type].splice(all[type].indexOf(handler) >>> 0, 1);
}
},
emit(type, evt) {
(all[type] || []).slice().map((handler) => {
handler(evt);
});
(all['*'] || []).slice().map((handler) => {
handler(type, evt);
});
}
};
}
module.exports = {
mitt: mitt
}
app.js的代码增加:
const mitt = require('./utils/mitt').mittpage里的代码增加
App({
onLaunch: function () {
let that = this
that.globalData.bus = mitt()
//调用page里的_custommethod
that.globalData.bus.emit('_custommethod', data)
}
})
const app = getApp()
Page({
custommethod: function(data){
},
onShow: function () {
let that = this
app.globalData.bus.on('_custommethod', that.custommethod)
},
onHide: function () {
let that = this
app.globalData.bus.off('_custommethod', that.custommethod)
},
})
网友回复
python如何调用openai的api实现知识讲解类动画讲解视频的合成?
html如何直接调用openai的api实现海报可视化设计及文本描述生成可编辑海报?
f12前端调试如何找出按钮点击事件触发的那段代码进行调试?
abcjs如何将曲谱播放后导出mid和wav格式音频下载?
python如何将曲子文本生成音乐mp3或wav、mid文件
python中mp3、wav音乐如何转成mid格式?
js在HTML中如何将曲谱生成音乐在线播放并下载本地?
python如何实现在windows上通过键盘来模拟鼠标操作?
python如何给win10电脑增加文件或文件夹右键自定义菜单?
python如何将音乐mp3文件解析获取曲调数据?