Vue message subscription and publishing pubsub

Vue message subscription and publishing pubsub

A way of communication between components, suitable for any communication between components.
Steps for usage:

  1. Install pubsub:npm i pubsub-js
  2. Introduce:import pubsub from 'pubsub-js'
  3. Receive data: If A component wants to receive data, it subscribes to the message in A component, and the subscribed callback stays in A component itself.
methods:{
    
    
	demo(data){
    
    
	}
}
mounted(){
    
    
	this.pid = pubsub.subscribe('xxx',this.demo)  //订阅消息
}
  1. provide data:pubsub.publish('xxx',数据)
  2. Finally, in the beforeDestroy hook, pubsub.unsubscribe(pid)unsubscribe with

Guess you like

Origin blog.csdn.net/m0_48546501/article/details/130618956