当前位置:网站首页>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 ”
边栏推荐
- 腾讯云搭建Socks5多IP代理服务器实现游戏单窗口单IP完美搭建教程附带工具「建议收藏」
- Variables, scopes, and variable promotion
- 阻止深度神经网络过拟合(Mysteries of Neural Networks Part II)
- [world history] Episode II: Dawn of civilization
- What if the CPU temperature of Dell computer is too high
- TSDB在民机行业中的应用
- Is it safe for tongdaxin to open a stock account
- None of the MLIR optimization passes are enabled (registered 2) solutions
- 2020-03-20
- H5 page graying source code, ie compatible (elegant downgrade provides download browser link)
猜你喜欢

Clinical Chemistry | 张建中/徐健开发幽门螺杆菌单细胞精准诊疗技术

Solving error: creating window glfw error: glew initialization error: missing GL version

Variables, scopes, and variable promotion
![[untitled] the CMD command window displays' NPM 'which is not an internal or external command](/img/db/b1ae4b0d1110af1e24887ba3b4fe16.jpg)
[untitled] the CMD command window displays' NPM 'which is not an internal or external command

电脑必须打开的设置

第一次读 “Clean” 系列,并没有觉得这是一本多好的书

Pourquoi les programmeurs devraient - ils être plus doux?

重磅!国产 IDE 发布,由阿里研发,完全开源!(高性能+高定制性)

Shell string variable

专家建议|8大措施加速你的创新职业规划和成长
随机推荐
laravel8实现图片验证码
国信证券股票账户开户安全吗?请问。
Share the code technology points and software usage of socket multi client communication
Is it unsafe to make new debts
Haven't you understood the microservice data architecture transaction management +acid+ consistency +cap+base theory? After reading it, you can completely solve your doubts
JS functions
Garbage collection mechanism
Which is better and safer, GF easy gold rush or flush
权益NFT开创者Hash Eagle如何重新定义NFT,用权益赋能长续价值?
BACnet gateway bl103 for building automation
Network remote access using raspberry pie
How to choose a technology stack for web applications in 2022
None of the MLIR Optimization Passes are enabled (registered 2)解决办法
分享自己平時使用的socket多客戶端通信的代碼技術點和軟件使用
合宙Air32F103CBT6开发板上手报告
Settings the PC must be turned on
Test your earning power? What will you do in the future?
Stream竟然还有应用进阶学习?作为程序员的你知道吗
JVM uses tools to analyze classic cases of OOM
Discriminative v.s.Generative