参考网站:https://www.npmjs.com/package/mobx-event-bus2
安装依赖包
npm install mobx-event-bus2 --save 或者 yarn add mobx-event-bus2
使用情景:
子组件 --CalcPeriodPanel.tsx
- 引入依赖包
import { eventBus, subscribe } from "mobx-event-bus2";
- 构造函数里注册eventbus
eventBus.register(this);
- 在逻辑代码方法里面传值
eventbus.post('方法名',值)
- 订阅传过来的值 @subscribe("传值过来的事件名“)
@subscribe("传值过来的事件名“)
事件
// loadingComplete作为事件订阅者处理所有的LoadingComplete事件
@subscribe("LoadingComplete")
loadingComplate(evt: any) {
console.log("CalcPeriodPanel -> reCalcComplete -> evt", evt);
this.setState({ loading: false });
}
父组件 --CalcTable.tsx
- 引入依赖包
import { eventBus, subscribe } from "mobx-event-bus2";
- 构造函数里注册eventbus
eventBus.register(this);
- 在逻辑代码方法里面传值
eventbus.post('方法名',值)
- 订阅传过来的值
@subscribe("传值过来的事件名“)
事件
以上是个人理解,应该有不对的地方,欢迎指正