当前位置:网站首页>25.消息订阅与发布——PubSub-js
25.消息订阅与发布——PubSub-js
2022-07-24 07:18:00 【风落不归处】
1.什么是PubSub-js?
PubSub.js是一个用Javascript编写的基于主题的发布/订阅库。
PubSubJS有同步解耦,所以主题是异步开发的。这有助于使你的程序保持可预测性,因为在消费者处理主题时,主题的发起者不会被阻止。
2.主要特点
无依赖
同步解耦
ES3兼容。PubSub-js能够在任何可以执行JavaScript的地方运行。浏览器、服务器、电子阅读器、手机、游戏机
AMD/Common.JS模块支持
不修改订阅者(jquery自定义事件修改订阅者)
易于理解和使用
小于1kb压缩
3.安装PubSubJS
有几种方法可以获取PubSubJS
1.通过npm(npm install pubsub-js)安装
4.使用PubSubJS
School组件
<template>
<div class="school">
</div>
</template>
<script>
import pubsub from 'pubsub-js'
export default {
name:'School',
data() {
return {
}
},
mounted() {
this.pubId = pubsub.subscribe('hello',(msgName,data)=>{
console.log(this)
console.log('有人发布了hello消息,hello消息的回调执行了',msgName,data)
})
},
beforeDestroy() {
pubsub.unsubscribe(this.pubId)
},
}
</script>
<style scoped>
.school{
background-color: skyblue;
padding: 5px;
}
</style>Student组件
<template>
<div class="student">
<button @click="sendStudentName">把学生名给School组件</button>
</div>
</template>
<script>
import pubsub from 'pubsub-js'
export default {
name:'Student',
data() {
return {
studentName: "张三"
}
},
mounted() {
},
methods: {
sendStudentName(){
pubsub.publish('hello',this.studentName)
}
},
}
</script>
<style lang="less" scoped>
.student{
background-color: pink;
padding: 5px;
margin-top: 30px;
}
</style>
App组件
<template>
<div class="app">
<School/>
<Student/>
</div>
</template>
<script>
import Student from './components/Student'
import School from './components/School'
export default {
name:'App',
components:{School,Student},
data() {
return {
}
}
}
</script>
<style scoped>
.app{
background-color: gray;
padding: 5px;
}
</style>
边栏推荐
- 【时序逻辑电路】——寄存器
- Paper reading: hardnet: a low memory traffic network
- FPGA实现AXI4总线的读写
- In the era of e-commerce, what should enterprises do in the transformation of social e-commerce?
- 【FreeRTOS】11 软件定时器
- What kind of mode can make platform users self-help fission- Chain 2+1
- Vulnhub DC1
- numpy.inf
- Aggregated new ecological model - sharing purchase, membership and reward system
- Kali安装pip以及pip换源
猜你喜欢

C language from introduction to soil -- super detailed summary of operators

Jay Chou's live broadcast was watched by more than 6.54 million people, with a total interaction volume of 450million, helping Kwai break the record again

Pytorch deep learning practice lesson 10 / assignment (basic CNN)
![[leetcode simple] 20. Valid brackets stack](/img/49/b3dce4ee0987c98f195e0f7f558d8f.png)
[leetcode simple] 20. Valid brackets stack

Blockbuster live broadcast | orb-slam3 series code explanation map points (topic 2)

深度学习二三事-回顾那些经典卷积神经网络

AMD64 (x86_64) architecture ABI document: upper

变量和数据类型(04)完结

JMeter notes 2 | JMeter principle and test plan elements
![[sequential logic circuit] - register](/img/a5/c92e0404c6a970a62595bc7a3b68cd.gif)
[sequential logic circuit] - register
随机推荐
Compilation and debugging (GCC, g++, GDB)
MySQL语句
JS_实现多行文本根据换行分隔成数组
Empty cup mentality, start again
C language from entry to Earth - array
Take you step by step to learn C (one)
Filter filter
C language from entry to soil function
FPGA realizes reading and writing of axi4 bus
win10声音图标有个没有声音
Single Gmv has increased 100 times. What is the "general rule" behind the rise of popular brands?
【FreeRTOS】11 软件定时器
17. What is the situation of using ArrayList or LinkedList?
中国三氯氢硅市场预测及战略研究报告(2022版)
使用堡垒机(跳板机)登录服务器
Chapter007-FPGA学习之IIC总线EEPROM读取
Talk about your thoughts about the future
Variables and data types (04) end
Take you to learn C step by step (second)
文件上传下载Demo