【问题标题】:Property 'onload' does not exist on type <ImgHTMLAttributes><ImgHTMLAttributes> 类型不存在属性“onload”
【发布时间】:2018-09-21 07:09:53
【问题描述】:

有我在打字稿中使用图像标签的代码

<img
  className="mediaImg"
  src={imageUrl}
  onload={() => console.log('dffffffffffffffffffffffff')}
  style={{ ...theme.messages.mediaImg }}
/>

当我在那里使用onload 属性时,我得到了错误

Property 'onload' does not exist on type 'DetailedHTMLProps<ImgHTMLAttributes<HTMLImageElement>, HTMLImageElement>'

任何帮助将不胜感激!!! 谢谢

【问题讨论】:

  • 我认为这只是一个错字onLoad(带有上L)
  • 然后我得到这个Type 'void' is not assignable to type '((event: SyntheticEvent&lt;HTMLImageElement&gt;) =&gt; void) | undefined'.

标签: reactjs typescript react-tsx


【解决方案1】:

您必须拨打onLoad 而不是onload

<img
 className="mediaImg"
 src={imageUrl}
 onLoad={() => console.log('dffffffffffffffffffffffff')}
 style={{ ...theme.messages.mediaImg }}
/>

【讨论】:

  • 然后我得到这个Type 'void' is not assignable to type '((event: SyntheticEvent&lt;HTMLImageElement&gt;) =&gt; void) | undefined'.
  • 我没有收到任何错误或警告,可能是您的style={{ ...theme.messages.mediaImg }}有问题
  • 我没有在 onLoad 函数中返回任何内容,我在代码中集成了 eslint。
  • 您好还有一个问题,我如何在 typescript 中将 prop 从一个组件传递到另一个组件?
  • google 一下,你会得到很多参考。在这里查看 -> stackoverflow.com/questions/22639534/…
猜你喜欢
  • 2019-11-26
  • 1970-01-01
  • 2021-01-09
  • 2021-07-10
  • 2022-01-07
  • 2022-01-27
  • 2023-03-18
  • 2023-02-16
相关资源
最近更新 更多