当前位置:网站首页>PubSub JS library realizes "cross component" data transfer
PubSub JS library realizes "cross component" data transfer
2022-06-25 14:23:00 【Fat goose 68】
List of articles
One 、 Reference resources
Application scenario description
stay React and Vue In development , If you encounter data transfer between peer components or across multi-level components , have access to pubsub-js Library implementation “ Subscribe and publish ” The ability of , Realization “ Data transfer across components ”
Instructions
Quick start ( subscribe —— Release )
// Reference resources https://www.npmjs.com/package/pubsub-js
import PubSub from 'pubsub-js'
/** * create a function to subscribe to topics * @param {*} msg Name of subject , In the case of msg yes 'MY TOPIC' * @param {*} data Parameters passed by the subscription topic */
var mySubscriber = function (msg, data) {
console.log( msg, data );
};
// Subscribe to a 'MY TOPIC' The theme , The corresponding method is mySubscriber
var token = PubSub.subscribe('MY TOPIC', mySubscriber);
// Release theme 'MY TOPIC', namely The transmitted parameters are 'hello world! publish'
// Asynchronous push
PubSub.publish('MY TOPIC', 'hello world! publish');
// Synchronous push
PubSub.publishSync('MY TOPIC', 'hello world! publishSync');
test('pubsubjs demo', (done) => {
// Asynchronous push
// PubSub.publish('MY TOPIC', 'hello world! publish');
setTimeout(()=>{
done()
}, 2000)
})
The publication matches multiple subscriptions The rules
// Reference resources https://www.npmjs.com/package/pubsub-js
import PubSub from 'pubsub-js'
var myFunc1 = function (msg, data) {
console.log( msg, 'myFunc1', data );
};
var myFunc2 = function (msg, data) {
console.log( msg, 'myFunc2', data );
};
var myFunc3 = function (msg, data) {
console.log( msg, 'myFunc3', data );
};
test('pubsubjs demo', (done) => {
PubSub.subscribe('a', myFunc1);
PubSub.subscribe('a.b', myFunc2);
PubSub.subscribe('a.b.c', myFunc3);
// PubSub.publish('a')
// Will trigger a method myFunc1
// PubSub.publishSync('a', 'publishSync')
// Three methods are triggered
// PubSub.publishSync('a.b.c', 'publishSync')
// Smart trigger myFunc3 Method
// PubSub.unsubscribe('a.b');
// PubSub.publishSync('a.b.c', 'publishSync')
// All subscriptions are cancelled , Neither method will happen
PubSub.clearAllSubscriptions();
PubSub.publishSync('a.b.c', 'publishSync')
setTimeout(()=>{
done()
}, 2000)
})
Publications match public and personalized subscriptions The rules
// Reference resources https://www.npmjs.com/package/pubsub-js
import PubSub from "pubsub-js";
test("pubsubjs demo", (done) => {
// create a subscriber to receive all topics from a hierarchy of topics
var myToplevelSubscriber = function (msg, data) {
console.log("top level: ", msg, data);
};
// subscribe to all topics in the 'car' hierarchy
PubSub.subscribe("car", myToplevelSubscriber);
// create a subscriber to receive only leaf topic from hierarchy op topics
var mySpecificSubscriber = function (msg, data) {
console.log("specific: ", msg, data);
};
// subscribe only to 'car.drive' topics
PubSub.subscribe("car.drive", mySpecificSubscriber);
// Publish some topics
PubSub.publish("car.purchase", {
name: "my new car" }); // Trigger myToplevelSubscriber
PubSub.publish("car.drive", {
speed: "14" }); // Trigger myToplevelSubscriber and mySpecificSubscriber
PubSub.publish("car.sell", {
newOwner: "someone else" }); // Trigger myToplevelSubscriber
setTimeout(() => {
done();
}, 2000);
});
Similar applications
Reference resources Vue2.x eventBus Manage events globally “ subscribe / Release ”
边栏推荐
- Hash table, hash conflict
- Reading the "clean" series for the first time, I didn't think it was a good book
- [world history] Episode 1: people in the Stone Age
- shell 变量 入门
- JS floating point multiplication and division method can not accurately calculate the problem
- 请问通达信股票开户是安全的吗?
- laravel8实现图片验证码
- Is it safe for tongdaxin to open a stock account
- Partager les points techniques de code et l'utilisation de logiciels pour la communication Multi - clients socket que vous utilisez habituellement
- K-line diagram 24 classic diagrams (shadow)
猜你喜欢

Nr-arfcn and channel grid, synchronous grid and GSCN

JS component

Summary of common functions in Oracle Database

Variables, scopes, and variable promotion

Partager les points techniques de code et l'utilisation de logiciels pour la communication Multi - clients socket que vous utilisez habituellement

多臺雲服務器的 Kubernetes 集群搭建

Preventing overfitting of deep neural networks (mysteries of neural networks Part II)

使用调试工具调试博图TCP连接所遇到的问题

数据采集系统网关采集工厂效率

112页机器学习-数学基础回顾.pptx
随机推荐
两种方法实现pycharm中代码回滚到指定版本(附带截图展示)
turtlebot+lms111+gmapping实践
Turtlebot+lms111+gmapping practice
多台云服务器的 Kubernetes 集群搭建
Kubernetes cluster construction of multiple ECS
合宙Air32F103CBT6开发板上手报告
哈希錶、哈希沖突
对白:推荐系统快速入门路线及各知识点总结
打新债是不是不安全
How to choose a technology stack for web applications in 2022
Shell string variable
JS component
第一次读 “Clean” 系列,并没有觉得这是一本多好的书
Qt内存映射
Discriminative v.s.Generative
sigmoid函数sigmoid求导
解决报错:Creating window glfw ERROR: GLEW initalization error: Missing GL version
Where can the brightness of win7 display screen be adjusted
What if the CPU temperature of Dell computer is too high
Experts' suggestions | 8 measures to accelerate your innovative career planning and growth