子组件调用父组件里的方法

子组件中有个button,添加点击事件如何能触发父组件中的方法。

HarmonyOS
2024-10-14 11:42:17
760浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
put_get

参考demo:

demo来实现子组件调用父组件里的方法:  
@Entry  
@Component  
struct Index12 {  
  clickFunc (data:number){  
    console.log(data.toString())  
  
  }  
  
  build() {  
    Row() {  
      Column() {  
        child({click:(data:number):void=>this.clickFunc(data)})  
      }  
      .width('100%')  
    }  
    .height('100%')  
  }  
}  
  
@Component  
export struct child {  
  
  click?=(data:number)=>{};  
  
  
  build() {  
    Row() {  
      Column() {  
        Text('点击子组件')  
          .onClick(()=>{  
            if (this.click != undefined ) {  
              this.click(123);  
            }  
          })  
  
      }  
      .width('100%')  
    }  
    .height('100%')  
  }  
  
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
分享
微博
QQ
微信
回复
2024-10-14 17:34:04


相关问题
组件调用组件方法
2220浏览 • 1回复 待解决
HarmonyOS 组件调用组件方法
876浏览 • 1回复 待解决
HarmonyOS 组件调用组件方法demo
845浏览 • 1回复 待解决
HarmonyOS 组件怎么调用组件方法
1211浏览 • 1回复 待解决