鸿蒙:配置事件

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

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

Button('Click me')
  .onClick(() => {
    this.myText = 'ArkUI';
  })

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

Button('add counter')
  .onClick(function(){
    this.counter += 2;
  }.bind(this))

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

myClickHandler(): void {
  this.counter += 2;
}
...
Button('add counter')
  .onClick(this.myClickHandler.bind(this))

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

fn = () => {
  console.info(`counter: ${this.counter}`)
  this.counter++
}
...
Button('add counter')
  .onClick(this.fn)

相关推荐

  1. 鸿蒙配置事件

    2024-02-03 02:02:02       60 阅读
  2. 鸿蒙 进程模型-公共事件

    2024-02-03 02:02:02       43 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-02-03 02:02:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-02-03 02:02:02       100 阅读
  3. 在Django里面运行非项目文件

    2024-02-03 02:02:02       82 阅读
  4. Python语言-面向对象

    2024-02-03 02:02:02       91 阅读

热门阅读

  1. gitlab 关闭Lets Encrypt证书续签

    2024-02-03 02:02:02       54 阅读
  2. harmony os系统

    2024-02-03 02:02:02       59 阅读
  3. 二分图板子

    2024-02-03 02:02:02       54 阅读
  4. numpy的学习之1

    2024-02-03 02:02:02       53 阅读
  5. Android 8.1 输入框返回键改为删除功能

    2024-02-03 02:02:02       52 阅读
  6. 面试手写第三期

    2024-02-03 02:02:02       55 阅读
  7. 网络通信--术语对照表

    2024-02-03 02:02:02       52 阅读
  8. ubuntu安装redis记录

    2024-02-03 02:02:02       62 阅读