中国优质的IT技术网站
专业IT技术创作平台
IT职业在线教育平台
微信扫码分享
@Entry @Component struct testPage { @State @Watch('change') person: Person = new Person('Peppa', 25, 'male') @State message: string = 'Hello World'; @State nameTemp: string = this.person.name @State ageTemp: number = this.person.age @State genderTemp: string = this.person.gender // change() { // console.log(`信息改变了:${JSON.stringify(this.person)}`) if (this.nameTemp !== this.person.name) { console.log(`名字信息改变了:${JSON.stringify(this.person.name)}`) //处理名字属性改变的逻辑/调用相关方法 } if (this.ageTemp !== this.person.age) { console.log(`年龄信息改变了:${JSON.stringify(this.person.age)}`) //处理年龄属性改变的逻辑/调用相关方法 } if (this.genderTemp !== this.person.gender) { console.log(`性别信息改变了:${JSON.stringify(this.person.gender)}`) //处理性别属性改变的逻辑/调用相关方法 } } build() { Column({ space: 10 }) { Text(`当前信息:${JSON.stringify(this.person)}`) Button() { Text('11点击一下') }.type(ButtonType.Capsule).padding({ left: 5, right: 5 }).onClick(() => { let person = new Person('Utopia', 25, 'female') this.person = person }) } .width('100%') .height('100%') .justifyContent(FlexAlign.Center) } } class Person { constructor(name: string, age: number, gender: string) { this.name = name; this.age = age; this.gender = gender; } name: string age: number gender: string }