?消息訂閱與發(fā)布(pubsub)
1. ??一種組件間通信的方式翩隧,適用于<span style="color:red">任意組件間通信</span>呻纹。
2. 使用步驟:
1. 安裝pubsub:```npm i pubsub-js```
2. 引入: ```import pubsub from 'pubsub-js'```
3. 接收數(shù)據(jù):A組件想接收數(shù)據(jù),則在A組件中訂閱消息雷酪,訂閱的<span style="color:red">回調(diào)留在A組件自身。</span>
```js
methods(){
demo(data){......}
}
......
mounted() {
this.pid = pubsub.subscribe('xxx',this.demo) //訂閱消息
}
```
4. 提供數(shù)據(jù):```pubsub.publish('xxx',數(shù)據(jù))```
5. 最好在beforeDestroy鉤子中太闺,用```PubSub.unsubscribe(pid)```去<span style="color:red">取消訂閱。</span>