鸿蒙:配置事件

事件方法以"."链式调用的方式配置系统组件支持的事件,建议每个事件方法单独写一行。

(1)使用箭头函数配置组件的事件方法。

TypeScript 复制代码
Button('Click me')
  .onClick(() => {
    this.myText = 'ArkUI';
  })

(2)使用匿名函数表达式配置组件的事件方法,要求使用bind,以确保函数体中的this指向当前组件。

TypeScript 复制代码
Button('add counter')
  .onClick(function(){
    this.counter += 2;
  }.bind(this))

(3)使用组件的成员函数配置组件的事件方法。

TypeScript 复制代码
myClickHandler(): void {
  this.counter += 2;
}
...
Button('add counter')
  .onClick(this.myClickHandler.bind(this))

(4)使用声明的箭头函数,可以直接调用,不需要bind this

TypeScript 复制代码
fn = () => {
  console.info(`counter: ${this.counter}`)
  this.counter++
}
...
Button('add counter')
  .onClick(this.fn)
相关推荐
ITUnicorn11 小时前
【HarmonyOS 6】进度组件实战:打造精美的数据可视化
华为·harmonyos·arkts·鸿蒙·harmonyos6
松叶似针13 小时前
Flutter三方库适配OpenHarmony【secure_application】— 五平台隐私保护机制横向对比
flutter·harmonyos
平安的平安14 小时前
【OpenHarmony】React Native鸿蒙实战:SegmentControl 分段控件详解
react native·react.js·harmonyos
平安的平安15 小时前
【OpenHarmony】React Native鸿蒙实战:ProgressRing 环形进度详解
react native·react.js·harmonyos
平安的平安15 小时前
【OpenHarmony】React Native鸿蒙实战:ProgressBar 进度条详解
react native·react.js·harmonyos
前端不太难15 小时前
未来的鸿蒙 App,还需要“首页”吗?
华为·状态模式·harmonyos
平安的平安15 小时前
【OpenHarmony】React Native鸿蒙实战:SearchBar 搜索栏详解
react native·react.js·harmonyos
HwJack201 天前
HarmonyOS APP UI单位适配深度实践:vp/fp/px的工程化解决方案分享
ui·华为·harmonyos
无巧不成书02181 天前
【RN鸿蒙教学|第10课时】应用异常处理+性能优化实战:夯实稳定性,备战打包部署
react native·华为·性能优化·交互·harmonyos
键盘鼓手苏苏1 天前
Flutter for OpenHarmony:git 纯 Dart 实现的 Git 操作库(在应用内实现版本控制) 深度解析与鸿蒙适配指南
开发语言·git·flutter·华为·rust·自动化·harmonyos