class ComponentController{
callback?:()=>void
}
@Entry
@ComponentV2
struct Index2 {
@Local title: string = "Titile One";
@Local fontColor: Color = Color.Red;
@Local componentController:ComponentController=new ComponentController()
aboutToAppear(): void {
this.componentController.callback=()=>{
this.title = `Title Update${Date.now()}`;
this.fontColor = Color.Red;
}
}
build() {
Column() {
EventChild({
title: this.title,
fontColor: this.fontColor,
componentController:this.componentController,
})
}
}
}
@ComponentV2
struct EventChild {
@Param title: string = '';
@Param fontColor: Color = Color.Black;
@Param @Require componentController: ComponentController;
build() {
Column() {
Text(`${this.title}`).fontColor(this.fontColor)
Button("子组件告诉父组件要更新数据")
.onClick(() => {
this.componentController.callback?.()
})
}
}
}