HarmonyOS 自定义组件如何把点击事件回调给给父组件

import { YTabEntity } from './YTabEntity'

@Component
export struct YTabBar {
  scroller: Scroller = new Scroller()
  @State selectIndex: number = 0
  @Require @Prop tabData: Array<YTabEntity> = []
  @Prop tabHeight: Length = 44
  @Prop tabSpace:number|string = 28
  @Prop color: ResourceColor = "#333333"
  @Prop selectColor: ResourceColor = "#1689FF"
  @Prop  onTabSelect:(index:number)=>void
  build() {
    List({ scroller: this.scroller,space:this.tabSpace }) {
      ForEach(this.tabData, (item: YTabEntity, index: number) => {
        ListItem() {
          Column() {
            Text(item.showText)
              .fontColor(this.selectIndex === index ? this.selectColor : this.color)
              .fontWeight(this.selectIndex === index ? FontWeight.Medium : FontWeight.Regular)
              .fontSize(16)
              .textAlign(TextAlign.Center)
              .layoutWeight(1)
              .onAreaChange((oldValue: Area, newValue: Area) => {
                item.textWidth = Number.parseInt(newValue.width.toString())
                console.info(`Ace: on area change, oldValue is${index}   ${JSON.stringify(oldValue)} value is ${JSON.stringify(newValue)}`)
              })
            Shape()
              .width(item.textWidth ?? 36)
              .height(2)
              .borderRadius(2)
              .backgroundColor(this.selectColor)
              .visibility(this.selectIndex === index ? Visibility.Visible : Visibility.Hidden)
          }.height('100%')
          .padding({ top: 6 })
        }.height('100%')
        .onClick(()=>{
          this.selectIndex = index
          // this.onTabSelect(index)
          const nextIndex = index+1
          if (nextIndex<this.tabData.length) {
            this.scroller.scrollToIndex(nextIndex,true,ScrollAlign.END)
          }
        })
      })
    }.height(this.tabHeight)
    .listDirection(Axis.Horizontal)
    .scrollBar(BarState.Off)
  }
}

如何把这个List item的点击事件回调给上一级

HarmonyOS
2024-12-25 08:24:49
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
fox280

可以定义一个controller类,在controller类中定义和子组件中类型相同的方法,在子组件中将实际封装的方法给到controller。

父组件在使用时,new一个controller对象然后转入子类中,在父组件中调用controller对应的方法即可

@Component
struct Child {
  @State private text: string = '初始值'
  private controller: ChildController = new ChildController();

  aboutToAppear() {
    // 子组件调用的方法为父组件传递过来的方法
    this.controller.testFunc('im the son')
    // 将testFunc方法用子组件方法进行覆盖
    if (this.controller) {
      this.controller.testFunc = this.testFunc
    }
  }

  // 子testFunc方法的具体实现
  testFunc = (value: string) => {
    this.text = value
    console.log('[testFunc]testFunc call from Child')
    return "[testFunc]我是儿子的方法"
  }

  build() {
    Column() {
      Text(this.text)
    }
  }
}

// 定义声明testFunc方法的controller
class ChildController {
  // 定义子testFunc方法同名的空方法
  testFunc = (value: string) => {
    console.log('[testFunc]testFunc: ' + value)
    return "[testFunc]我是公共定义的空方法"
  }
}

@Entry
@Component
struct Parent {
  private ChildRef = new ChildController()

  aboutToAppear(): void {
    this.ChildRef.testFunc = this.testFunc
  }

  // 父testFunc方法的具体实现
  testFunc = (value: string) => {
    console.log('[testFunc]我是父亲的testFunc方法 : ' + value)
    return "[testFunc]我是父亲的方法"
  }

  build() {
    Column() {
      Text('获取Child的exposedMethods!')
        .fontSize('18vp')
        .fontColor(Color.Gray)
      Divider()
      // 将父方法作为参数传递给子组件
      Child({ controller: this.ChildRef })
      // 父组件调用子组件的方法
      Button('Parent调用children的方法')
        .onClick(() => {
          let text = this.ChildRef.testFunc('Parent调用children的方法')
          console.info('[testFunc]testFunc info:' + JSON.stringify(text))
        })
    }
  }
}
分享
微博
QQ
微信
回复
2024-12-25 10:28:33
相关问题
HarmonyOS 组件是否支持自定义事件
268浏览 • 1回复 待解决
HarmonyOS UI组件自定义点击范围
557浏览 • 1回复 待解决
组件如何处理子组件点击事件
2881浏览 • 1回复 待解决
组件自定义调函数实现
696浏览 • 1回复 待解决
HarmonyOS 自定义组件事件处理
615浏览 • 1回复 待解决
HarmonyOS 定义自定义组件
245浏览 • 1回复 待解决
提问
该提问已有0人参与 ,帮助了0人