中国优质的IT技术网站
专业IT技术创作平台
IT职业在线教育平台
如何通过自定义公共事件实现进程间的通信?
微信扫码分享
subscriber: commonEventManager.CommonEventSubscriber | null = null; subscribeInfo: commonEventManager.CommonEventSubscribeInfo = { events: ["myEvent"]//自己定义的事件名称 }; aboutToAppear(): void { // 创建订阅者回调 commonEventManager.createSubscriber(this.subscribeInfo, (err: Base.BusinessError, data: commonEventManager.CommonEventSubscriber) => { if (err) { return; } this.subscriber = data; this.scribeSuccess = true if (this.subscriber !== null) { commonEventManager.subscribe(this.subscriber, (err: Base.BusinessError, data: commonEventManager.CommonEventData) => { if (err) { return; } console.info("==============success:" + JSON.stringify(data)) }) } }) } build() { Column() { Button("发布事件").onClick(() => { let options: commonEventManager.CommonEventPublishData = { code: 1, // 公共事件的初始代码 data: '我是携带的数据', // 公共事件的初始数据 parameters:[{"name":"zhangsan"}] }; commonEventManager.publish('myEvent', options, (err: Base.BusinessError) => { if (err) { console.info(`PublishCallBack err = ${JSON.stringify(err)}`); } else { console.info(`Publish success`); } }); }) }.width("100%") .justifyContent(FlexAlign.Center) .height("100%") } 打印日志: ==============success:{"event":"myEvent","bundleName":"","code":1,"data":"我是携带的数据","parameters":{"0":{"name":"zhangsan"},"length":1,"moduleName":""}}