通过@Observed配合@ObjectLink装饰符实现。@Observed用于类,@ObjectLink用于变量。
代码示例
在类上使用@Observed。
@Observed
class ClassA {
public name: string
public c: number
public id: number
constructor(c: number, name: string = 'OK') {
this.name = name
this.c = c
}
}
在组件变量使用@ObjectLink。
@Component
struct ViewA {
label: string = 'ViewA1'
@ObjectLink a: ClassA
build() {
Row() {
Button(`ViewA [${this.label}] this.a.c= ${this.a.c} +1`)
.onClick(() => {
this.a.c += 1
})
}.margin({ top: 10 })
}
}
完毕
联系客服