在使用@Provide修饰的变量进行emiit操作的时候,订阅方收到的数据为空。发送方和订阅方都是通过@Component修饰的页面,发送的数据使用provide修饰的数据。
示例代码如下:
@Entry
@Component
export struct BaseScanPage{
@Provide testResult:Array<string> = ["2333333"]
aboutToAppear() {
FMEmitter.onScanResult((result)=>{
console.log(JSON.stringify(result))
})
}
onResult(){
FMEmitter.emitScanResult(this.testResult)
}
}
/**
* 订阅 - 扫码回调
*/
onScanResult(callback: FMEmitterCallback<emitter.EventData>) {
this.on(scanSuccessBack, callback)
}
offScanResult() {
emitter.off(scanSuccessBack.eventId)
}
/**
* 发送 - 扫码成功回调
*/
emitScanResult(result?: Array<string>) {
let eventData: emitter.EventData = {
data: {
"result": result
}
}
this.emit(scanSuccessBack, eventData)
}
export const scanSuccessBack: emitter.InnerEvent = {
eventId: 72
}
调用onResult在接收处接收不到数据。
发送的@Provide修饰的变量可以被接收,示例参考如下:
参考文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/itc-with-emitter-V5