連結:PubSubJS
具體程式碼實現 :
//先安裝
yarn add pubsub-js
// 引入
import PubSub from 'pubsub-js'
//釋出:
PubSub.publish('zxc', 'hello world');
//訂閱: zxc名稱必須相同
token = PubSub.subscribe('zxc', (msg, data) => {
console.log('msg ', msg);
console.log('data ', data);
})
//取消訂閱:
PubSub.unsubscribe(token);
本作品採用《CC 協議》,轉載必須註明作者和本文連結