小程序全局事件监听及触发使用
解决app.globalData 全局变量更新触发页面及组件更新
创建目录:utils/events.js
var event = {};
const $on = function(params) {
if (!params) {
return false;
}
if (!params.name) {
console.error("事件监听未设置名称 属性key=name");
return false;
}
if (!params.success) {
console.error("事件监听未设置回调函数 属性key=success");
return false;
}
if (!params.tg) {
console.error("事件监听未设置目标对象 属性key=tg");
return false;
}
if (event[params.name]) {
var list = event[params.name];
list.push([params.tg, params.success]);
} else {
event[params.name] = [
[params.tg, params.success]
];
}
pageStatus(params.tg);
}
const $emit = function(params) {
if (!params) {
return false;
}
if (!params.name) {
console.error("事件发送未设置名称 属性key=name");
return false;
}
if (event[params.name]) {
var list = event[params.name];
list.forEach(item => {
item[1].call(item[0], params.data);
})
}
}
const $remove = function(params) {
if (!params) {
return false;
}
if (!params.tg) {
console.error("事件监听未设置目标对象 属性key=tg");
return false;
}
if (params.name && event[params.name]) {
event[params.name] = event[params.name].filter(a => {
return a[0] != params.tg;
})
} else {
for (let key in event) {
event[key] = event[key].filter(a => {
return a[0] != params.tg;
})
}
}
}
const pageStatus = function(self) {
if (self["onUnload"]) {
var s = self["onUnload"];
self["onUnload"] = function(a) {
s.call(this, a);
$remove({
tg: this
});
}
} else {
self["onUnload"] = function() {
$remove({
tg: this
});
}
}
}
exports.$on = $on;
exports.$emit = $emit;
exports.$remove = $remove;
使用:监听全局事件
import event from '../../utils/events.js'
Component(
{
lifetimes: {
attached: function() {
const that = this
let voiceState = getApp().globalData.voiceState
this.setData({ voiceState })
event.$on({
name: 'changeVoice',
tg: this,
success: (voiceState) => {
that.setData({ voiceState })
}
})
},
},
})
使用: 触发全局事件
<text
class="iconfont {{voiceState ? 'icon-shengyinkai' : 'icon-jingyin'}} video-voice"
bind:tap="triggerVoice"></text>
import event from '../../utils/events.js'
pages({
methods: {
triggerVoice() {
app.globalData.voiceState = !app.globalData.voiceState
event.$emit({
name: 'changeVoice',
data: app.globalData.voiceState
})
}
}
})