小白求助: openharmony开发中@Watch监听与事件订阅有什么区别?
·
最近在学习openharmony开发,对监听与订阅知识点感到迷惑。
(1)、之前学习用到了@Watch装饰器,用来对状态变量进行监听,如
@Prop @Watch('onCountUpdated') count: number = 0;
// @Watch 回调
onCountUpdated(propName: string): void {
this.total += this.count;
}
当变量 count的值发生改变时,状态管理框架会调用@Watch函数onCountUpdated,实现监听操作
(2)、最近进行蓝牙开发时,遇见一个订阅蓝牙设备开关状态事件,bluetoothManager.on(‘stateChange’)
官方给的示例是
function onReceiveEvent(data: bluetoothManager.BluetoothState) {
console.info('bluetooth state = '+ JSON.stringify(data));
}
try {
bluetoothManager.on('stateChange', onReceiveEvent);
} catch (err) {
console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message);
}
是不是这个订阅事件,也是相当于注册了一个监听事件?当蓝牙的开关状态发生改变,就会自动调用函数onReceiveEvent ?
想了解更深入的了解关于订阅和监听的知识,求大佬指点
更多推荐
所有评论(0)