下载
npm install pubsub-js --save
使用
订阅:
state = {pubsub:""}
componentDidMount() {
//订阅
this.state.pubsub = PubSub.subscribe("titleHandle", (msg, data) => {
console.log(data) // 传递的数据
})
}
componentWillUnmount() {
/**
* 取消指定的订阅
*/
PubSub.unsubscribe(this.state.pubsub)
/**
* 取消全部订阅
*/
PubSub.clearAllSubscriptions()
}
触发:
PubSub.publish('titleHandle', {name:"点击了",id:5})