You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用onClick函数触发ESLint报错:Expected method shorthand (object-shorthand)

解决ESLint的object-shorthand报错问题

这个报错是ESLint的object-shorthand规则在起作用——它要求对象中的方法使用简写语法,而非带function关键字的完整写法。

直接把代码里的方法声明改成简写形式就能解决:

onClick(c, i) {
  const e = i[0] // 这里建议加上const/let,避免未声明变量的额外ESLint报错
  console.log(e._index)
  const x_value = this.data.labels[e._index]
  const y_value = this.data.datasets[0].data[e._index]
  console.log(x_value)
  console.log(y_value)
}

额外提醒:原代码里e = i[0]没有声明变量,会隐式创建全局变量,加上const或let不仅符合规范,还能避免另一类ESLint报错。

内容的提问来源于stack exchange,提问作者Shahzad Qasir

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 17:52:01