当前位置:网站首页>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>
边栏推荐
- 变量和数据类型(03)
- Variables and data types (04) end
- 文件上传下载Demo
- 周杰伦直播超654万人观看,总互动量破4.5亿,助力快手再破纪录
- SPI - send 16 bit and 8-bit data
- File "manage.py", line 14) from exc ^ syntaxerror: cause and solution of invalid syntax error
- [tips] a simple method to create a version control project
- 【PTA】团体程序设计天梯赛-练习集 L3题目总结(不全)
- 使用堡垒机(跳板机)登录服务器
- 单场GMV翻了100倍,冷门品牌崛起背后的“通用法则”是什么?
猜你喜欢

变量和数据类型(03)

文件上传下载Demo

Stm32h750vbt6 drives programmable gain amplifier module pga113 -- Hal Library Based on cubemx

mysql查询当前节点的所有父级

JMeter笔记2 | JMeter原理及测试计划要素

Using depth and normal textures in unity

Part II - C language improvement_ 4. Secondary pointer
![[leetcode simple] 20. Valid brackets stack](/img/49/b3dce4ee0987c98f195e0f7f558d8f.png)
[leetcode simple] 20. Valid brackets stack

MongoDB应用场景及选型(海量数据存储选型)

单点登录的三种实现方式
随机推荐
Aggregated new ecological model - sharing purchase, membership and reward system
[leetcode] 444. Sequence reconstruction
From the perspective of CIA, common network attacks (blasting, PE, traffic attacks)
Basic syntax of MySQL DDL and DML and DQL
Vulnhub DC1
C language from entry to soil function
MongoDB应用场景及选型(海量数据存储选型)
Win10 sound icon has no sound
Variables and data types (04) end
Part II - C language improvement_ 4. Secondary pointer
What kind of mode can make platform users self-help fission- Chain 2+1
File “manage.py“, line 14 ) from exc ^ SyntaxError: invalid syntax出错的原因及解决方法
Wild pointer, null pointer, invalid pointer
Bookkeeping app: xiaoha bookkeeping 1 - production of welcome page
Oauth2==SSO三种协议。Oauth2四种模式
File upload and download demo
Riotboard development board series notes (IX) -- buildreoot porting matchbox
B. Also Try Minecraft
MySQL queries all parents of the current node
Compilation and debugging (GCC, g++, GDB)