鸿蒙:配置事件

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

(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)
相关推荐
FrameNotWork2 小时前
HarmonyOS应用《左右相册》 智感握姿实战:让操作按钮自动“站“到你拇指够得到的地方
华为·harmonyos
hacker7072 小时前
DBeaver 鸿蒙 PC 适配全记录:在 HarmonyOS 桌面端原生重建一个通用数据库工具
数据库·华为·harmonyos
hacker7073 小时前
DBeaver Community 鸿蒙 PC 适配全记录:在 HarmonyOS PC 上运行原生数据库管理工具
数据库·华为·harmonyos
HwJack204 小时前
用 HML + CSS + JS 三段式写鸿蒙界面
javascript·css·harmonyos
小雨青年13 小时前
【HarmonyOS 7 平行视界深度实战】06 页面路由、返回栈与多层跳转怎么处理
华为·harmonyos
2501_9197490319 小时前
华为鸿蒙免费刷题软件—小羊免费刷题
华为·harmonyos·鸿蒙
游戏智眼19 小时前
HarmonyOS 7 适配升级:NIM SDK 释放端侧 AI 与网络能力
人工智能·harmonyos
程序猿追20 小时前
react-native-elements 三方库鸿蒙版本适配与使用(MatePad Edge 双模式真机验证)
华为·harmonyos
lqj_本人21 小时前
白泽上手:给小鸿 SE 写一个温控风扇工程
harmonyos