HarmonyOS 自定义组件如何在布局层添加onClick事件等系统ui自带的方法

自定义组件如何在布局层添加onClick事件等系统UI自带的方法。

比如有一个自定义组件如下:

import { ExposureController } from './ExposureController'
class StackMF implements AttributeModifier<StackAttribute>{
}
@Component
export struct ExposureStack {
  mf:AttributeModifier<StackAttribute> = new StackMF()
  controller:ExposureController = new ExposureController({})
  itemPosition?:number
  data?:Object
  @BuilderParam builderParam:()=>void = this.buildCloser
  @Builder buildCloser(){
  }
  build() {
    Stack(){
      this.builderParam()
    }
    .onVisibleAreaChange([0,1],(isVisible,ratio)=>{
      this.controller.onChangeVisible(isVisible,ratio,this.itemPosition??0,this.data)
    })
    .attributeModifier(this.mf)
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.

然后在使用这个组件时想添加onClick事件。

ExposureStack({
  itemPosition: this.positionItem,
  data: this.video,
  controller: this.stackController,
  mf: this.videoStackMf
})
  .onClick(() => {
    this.videoControl.requestFullscreen(true);
  })
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.

但是这么书写会报错:

Cannot find name 'onClick'. Did you mean the instance member 'this.onClick'? 
  • 1.

请问如何让自定义组件具备系统UI组件的一些能力方法啊,比如onClick。

HarmonyOS
2024-12-24 16:53:23
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
shlp

自定义组件是可以添加onClick方法的下面是测试demo。可以在自定义组件外层套一个column,给column添加点击事件,或者直接在自定义组件内部直接添加onClick事件。

@Entry
@Component
struct Index {
  @State message: string = 'Hello World';
  build() {
    RelativeContainer() {
      component()
        .onClick(() => {
          console.log('hello')
        })
    }
    .height('100%')
    .width('100%')
  }
}
@Component
struct component {
  build() {
    Text('测试')
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
分享
微博
QQ
微信
回复
2024-12-24 19:01:56


相关问题
HarmonyOS 地图组件如何添加自定义UI
572浏览 • 1回复 待解决
HarmonyOS 如何自定义布局组件
729浏览 • 1回复 待解决
自定义组件如何添加图片?
3392浏览 • 1回复 待解决
HarmonyOS C++自定义组件如何开发?
1169浏览 • 1回复 待解决
HarmonyOS 自定义组件事件处理
1242浏览 • 1回复 待解决
如何在自定义函数中创建一个UI组件
2554浏览 • 1回复 待解决
HarmonyOS 组件是否支持自定义事件
819浏览 • 1回复 待解决
HarmonyOS自定义组件增加方法如何实现
1253浏览 • 1回复 待解决
HarmonyOS 键盘顶部添加自定义组件
964浏览 • 1回复 待解决
js 自定义组件如何传递方法
6724浏览 • 2回复 待解决