当前位置:网站首页>Wechat applet implements a global event bus by itself
Wechat applet implements a global event bus by itself
2022-07-23 22:58:00 【Big carp Yu】
Be similar to Vue Global event bus
Have a problem : Users click to modify avatars in their own small projects , The avatar of the home page needs to be modified synchronously .
Ideas
- First of all app Bind an attribute to the instance $bus
- Then create an array to store our events
- emit Release events , Accept three parameters ( Event name , function , and this Point to ) I practice here by myself because of small projects , Therefore, there is no legal parameter verification and event Whether it is repeated internally
- on Subscribe to custom events , Because it has been bound this So direct execution
- off Used to cancel events
export class Bus{
constructor(){
this.event = []
}
emit = (eventName,fn,that)=>{
this.event.push({
[eventName]:fn.bind(that)
})
}
on = (eventName,params)=>{
this.event.forEach((v,index)=>{
if(v[eventName]){
v[eventName](params)
console.log(' The custom event executes ');
}else{
console.error(" Custom event not found ",eventName)
}
})
}
off = (eventName)=>{
this.event.forEach((v,index)=>{
if(v[eventName]){
this.event.splice(index,1)
}
})
}
}
边栏推荐
- Programming in the novel [serial 20] the moon bends in the yuan universe
- [golang learning notes] is parameter transfer in go language value transfer or reference transfer
- As a developer, you have to know the three performance testing tools JMeter, API and jmh user guide
- TAP 系列文章9 | 应用开发加速器
- What if the content of software testing is too simple?
- Tools in the tools package of Damon database (operate Damon database)
- Ways to improve the utilization of openeuler resources 01: Introduction
- 视频号加强打击低俗内容:对违背公序良俗的内容必须赶尽杀绝
- Lu Xia action | Source Kai Digital: Existing Mode or open source innovation?
- $, $*, [email protected], $$ Understand the meaning of 0
猜你喜欢

Matlab wavelet toolbox import signal error (doesn't contain one dimensional single)

Raspberry pie SSH login
![[laser principle and Application-8]: EMC design of laser circuit](/img/98/8b7a4fc3f9ef9b7e16c63a8c225b02.png)
[laser principle and Application-8]: EMC design of laser circuit

synthesizable之Verilog可不可综合

SOLIDWORK learning notes: Sketch geometric relationships and editing

Microsoft SQL Server数据库语言及功能使用(十三)

Build your own target detection environment, model configuration, data configuration mmdetection

作为开发,你不得不知道的三个性能测试工具|Jmeter、Apipost、JMH使用指南

FL Studio 20.9 update Chinese version host Daw digital audio workstation

At the forefront of the times, Huawei aims at the wind and sea of digital finance
随机推荐
Upgrade unity visual studio 2019 to 2022 (throw away pirated red slag)
dried food! Implicit sparse regularization effect in neural networks
[laser principle and Application-8]: EMC design of laser circuit
TAP 系列文章8 | TAP 学习中心——通过动手教程来学习
Diabetes genetic risk testing challenge baseline
"Morning reading" if you were in my position, what would you do? How do we do it,
What else do entrepreneurs need besides money? Exclusive interview with Mingyue Lake venture capital institutions
Leetcode: palindrome number
1000个Okaleido Tiger首发上线Binance NFT,引发抢购热潮
[golang learning notes] is parameter transfer in go language value transfer or reference transfer
Tap series article 4 | backstage based tap developer portal
疯狂的牛市,下半场何去何从?2021-04-30
How to reasonably estimate the size of thread pool
Mongodb - Introduction to the usage of logical operators not, and, or, nor in query statements
ospf终极实验——学会ospf世纪模板例题
Data sorting and usage before torchvision.datasets.imagefolder
How can I open an account to buy financial products with a 6% income?
DeFi項目的盈利邏輯 2021-04-26
Matlab小波工具箱导入信号出错(doesn‘t contain one dimensional Singal)
Programming in the novel [serial 17] the moon bends in the yuan universe