HarmonyOS 是否有类似vue里的子组件通过emit触发父组件里方法的api ?

HarmonyOS 是否有类似vue里的子组件通过emit触发父组件里方法的api ?

HarmonyOS
2024-11-26 09:24:22
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
aquaa
//父组件调用子组件的方法参考代码: 
 
@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%") 
  } 
} 
 
 
//子组件调用父组件里的方法: 
@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.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
分享
微博
QQ
微信
回复
2024-11-26 16:14:09
相关问题
组件调用组件方法
1143浏览 • 1回复 待解决
组件调用组件方法
2255浏览 • 1回复 待解决
HarmonyOS 组件怎么调用组件方法
1260浏览 • 1回复 待解决
HarmonyOS 组件调用组件方法demo
890浏览 • 1回复 待解决
HarmonyOS 组件调用组件方法
918浏览 • 1回复 待解决
能否使用类似csscalc方法
1302浏览 • 1回复 待解决