微信小程序如何写一个可以订阅消息的代码?只要在app.js里接受到服务器消息,就可以按照各个page的订阅消息进行回调
网友回复
在utils下新建一个Topic.js文件,代码如下
class Topic {//主题
constructor() {
this.observers = [];//存储订阅者
}
subit(observer) {//增加订阅者
this.observers.push(observer);
}
unsubit(observer) {//删除订阅者
this.observers.forEach((item, index) => {
if (item == observer) {
this.observers.splice(index, 1);
return;
}
});
}
notify(data) {// 向订阅者发布消息
this.observ...点击查看剩余70%
1、新建一个Publishtion.js
class Publishtion {
subscribers = {
any: []
}
// 添加订阅者 订阅者 = 注册方法
on(type,fn) {
type = type || 'any'
if (typeof this.subscribers[type] === 'undefined') {
this.subscribers[type] = []
}
this.subscribers[type].push(fn)
}
// 移除订阅者
remove() {
// 传入参数 移除的指令 移除的用户 对应的订阅消息
this.visitSubscribers('unsubscribe', fn, type)
}
// 发布消息
publish(publication, type) {
// 传入参数 发布消息的指令 发布的内容 发布到具体哪个类目
this.visitSubscribers('publish', publication, type)
}
// 访问订阅库 ...点击查看剩余70%
有没有不依赖embedding向量的RAG技术?
有没有支持实时打断语音通话并后台帮你执行任何的ai模型?
开源ai大模型文件格式GGUF、MLX、Safetensors、 ONNX 有什么区别?
出海挣钱支付收款PayPal、Wise 、PingPong、Stripe如何选择?
如何实现类似google的图片隐形水印添加和识别技术?
linux上如何运行任意windows程序?
ai能写出比黑客还厉害的零日漏洞等攻击工具攻击任意软件系统工程?
js如何获取浏览器的音频上下文指纹、Canvas指纹、WebGL渲染特征?
为啥ai开始抛弃markdown文本,重新偏好html文本了?
网站有没有办法鉴别访问请求是由ai操控chrome-devtools-mcp发出的?


