中国优质的IT技术网站
专业IT技术创作平台
IT职业在线教育平台
微信扫码分享
@Component struct Child { @State private text: string = '初始值' private controller: ChildController = new ChildController(); aboutToAppear() { if(this.controller) { //给controller对应的方法赋值 this.controller.changeText = this.changeText } } //封装的能力 private changeText = (value: string) =>{ this.text = value } build() { Column() { Text(this.text) } } } //定义controller对象 class ChildController { changeText = (value: string) => {} } @Entry @Component struct Parent { private ChildRef = new ChildController() build() { Column() { Text('调用Child的changeText').fontSize('18vp').fontColor(Color.Gray) Divider() Child({ controller:this. ChildRef }) Button('Parent调用childer的changeText').onClick(() => { this.ChildRef.changeText('Parent调用childer的changeText') }) } .justifyContent(FlexAlign.Center) .width("100%") .height("100%") } }