中国优质的IT技术网站
专业IT技术创作平台
IT职业在线教育平台
微信扫码分享
// 导入模块 import distributedDataObject from '@ohos.data.distributedDataObject'; class People { name: string|undefined age: number |undefined= 0 constructor(name: string|undefined, age: number|undefined) { this.name = name this.age = age } } @Entry @Component struct Index { build() { Column() { Button("创建分布式对象").onClick(() => { let people = new People(undefined,undefined) let localObject: distributedDataObject.DataObject = distributedDataObject.create(getContext(this), people); localObject.setSessionId("123456")//设置sessionId //监听变化 localObject.on("change", (sessionId: string, fields: Array<string>) => { console.info("change" + sessionId); if (fields != null && fields != undefined) { for (let index: number = 0; index < fields.length; index++) { console.info(`The element ${localObject[fields[index]]} changed.`); } } }); }) } .width('100%') .height('100%') .justifyContent(FlexAlign.Center) } }