当前位置:网站首页>25. Message subscription and publishing - PubSub JS
25. Message subscription and publishing - PubSub JS
2022-07-24 07:25:00 【Where the wind falls】
1. What is? PubSub-js?
PubSub.js It's a use. Javascript Topic based publishing written by / Subscription library .
PubSubJS With synchronous decoupling , So the theme is developed asynchronously . This helps keep your program predictable , Because when consumers deal with topics , The originator of the theme will not be blocked .
2. The main features
Without relying on
Synchronous decoupling
ES3 compatible .PubSub-js Be able to execute at any time JavaScript Where to run . browser 、 The server 、 E-readers 、 mobile phone 、 console
AMD/Common.JS Module support
Do not modify subscribers (jquery Custom events modify subscribers )
Easy to understand and use
Less than 1kb Compress
3. install PubSubJS
There are several ways to get PubSubJS
1. adopt npm(npm install pubsub-js) install
4. Use PubSubJS
School Components
<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(' Someone has released hello news ,hello The callback of the message executes ',msgName,data)
})
},
beforeDestroy() {
pubsub.unsubscribe(this.pubId)
},
}
</script>
<style scoped>
.school{
background-color: skyblue;
padding: 5px;
}
</style>Student Components
<template>
<div class="student">
<button @click="sendStudentName"> Give the students their names School Components </button>
</div>
</template>
<script>
import pubsub from 'pubsub-js'
export default {
name:'Student',
data() {
return {
studentName: " Zhang San "
}
},
mounted() {
},
methods: {
sendStudentName(){
pubsub.publish('hello',this.studentName)
}
},
}
</script>
<style lang="less" scoped>
.student{
background-color: pink;
padding: 5px;
margin-top: 30px;
}
</style>
App Components
<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>
边栏推荐
- Buffer overflow vulnerability of network security module B in national vocational college skills competition
- 研究会2022.07.22--对比学习
- AMD64 (x86_64) architecture ABI document: upper
- 【PTA】团体程序设计天梯赛-练习集 L3题目总结(不全)
- Jackson 解析 JSON 详细教程
- Give a string ① please count the number of times each letter appears ② please print the pair with the most letters
- In the era of e-commerce, what should enterprises do in the transformation of social e-commerce?
- fopen、fwrite、fseek、ftell、fread使用demo
- Part II - C language improvement_ 4. Secondary pointer
- Learning notes - distributed transaction theory
猜你喜欢

Riotboard development board series notes (IX) -- buildreoot porting matchbox

我的创作纪念日

文件上传下载Demo

Vulnhub DC1

Compilation and debugging (GCC, g++, GDB)

Part II - C language improvement_ 2. Memory partition

Jenkins 详细部署

QoS服务质量四QoS边界行为之流量监管

Injectfix principle learning (to realize the heat of repair addition)

Vulnhub DC1
随机推荐
[PTA] group programming ladder competition - Summary of exercises L3 (incomplete)
【时序逻辑电路】——寄存器
From the perspective of CIA, common network attacks (blasting, PE, traffic attacks)
C language from introduction to soil -- super detailed summary of operators
Influxdb未授权访问&CouchDB权限绕过
MITRE ATT&CK超详细学习笔记-02(大量案例)
Chinese manufacturers may catch up with the humanoid robot Optimus "Optimus Prime", which has been highly hyped by musk
24.全局事件总线
Part II - C language improvement_ 4. Secondary pointer
Jackson 解析 JSON 详细教程
cookie_ session
Chapter007 FPGA learning IIC bus EEPROM reading
Development system selection route
cookie_session
Vulnhub DC1
Injectfix principle learning (to realize the heat of repair addition)
Mongodb application scenario and model selection (massive data storage model selection)
The goal you specified requires a project to execute but there is no POM in this directory
JS的DOM操作——style的操作
cloud的版本升级