Postal.js 订阅未收到来自其他 React 组件的 post
Postal.js subscription not receiving post from other react component
我在一页上有两个解耦组件 运行,我想使用 Postal.js 在点击其中一个时向另一个发送消息。
单击第一个组件时,它会发布 post:
postal.publish({
channel: 'carts',
topic: 'item.add',
data: {
quantity: newQuantity
}
});
然后在第二个组件中,我在 componentDidMount():
中订阅了那个频道
postal.subscribe({
channel: 'carts',
topic: 'item.add',
callback: this.handleStorageChange.bind(this)
});
当我向订阅的组件添加窃听器时,它显示频道已成功订阅,但从未显示来自其他组件的 post:
{"channel":"postal","topic":"subscription.created","data":{"event":"subscription.created","channel":"carts","topic":"item.add"},"timeStamp":"2018-03-14T01:59:38.309Z"}
当我向 posting 组件添加窃听器时,它显示消息是 posted:
{"channel":"carts","topic":"item.add","data":{"quantity":34},"timeStamp":"2018-03-14T01:59:41.844Z"}
我做错了什么?
我尝试创建一个最小的片段来在两个组件之间进行通信。而且我能够收到订阅组件的数据。
你可以参考这个片段,https://codesandbox.io/s/kxqw6npm05
原来我的两个组件在不同的 webpack 包中导致两者之间的通信出现问题。
将两个组件打包在一起解决了这个问题。
我在一页上有两个解耦组件 运行,我想使用 Postal.js 在点击其中一个时向另一个发送消息。
单击第一个组件时,它会发布 post:
postal.publish({
channel: 'carts',
topic: 'item.add',
data: {
quantity: newQuantity
}
});
然后在第二个组件中,我在 componentDidMount():
中订阅了那个频道postal.subscribe({
channel: 'carts',
topic: 'item.add',
callback: this.handleStorageChange.bind(this)
});
当我向订阅的组件添加窃听器时,它显示频道已成功订阅,但从未显示来自其他组件的 post:
{"channel":"postal","topic":"subscription.created","data":{"event":"subscription.created","channel":"carts","topic":"item.add"},"timeStamp":"2018-03-14T01:59:38.309Z"}
当我向 posting 组件添加窃听器时,它显示消息是 posted:
{"channel":"carts","topic":"item.add","data":{"quantity":34},"timeStamp":"2018-03-14T01:59:41.844Z"}
我做错了什么?
我尝试创建一个最小的片段来在两个组件之间进行通信。而且我能够收到订阅组件的数据。
你可以参考这个片段,https://codesandbox.io/s/kxqw6npm05
原来我的两个组件在不同的 webpack 包中导致两者之间的通信出现问题。
将两个组件打包在一起解决了这个问题。