如何使用 Next/Image 获取 img 元素的宽度和高度属性

发布于 2025-01-18 20:38:07 字数 593 浏览 5 评论 0原文

图像正常工作。这是代码

<Image
  layout="fixed"
  src="/images/example.jpeg"
  alt="Example image"
  width="140"
  height="140"
 />

,但是,当我在web.dev上运行网站时,我不会获得性能的顶级。 主要原因是 图像元素没有明确的宽度和高度

我已经研究过,并且可以从这里分辨出 https://web.dev/poptimize-cls/jutm_sourm_sourm_source = lighthouse&amp.amp.amp.amp.amm_medium_medium = lrrrrrrr.images-without - 维度 该宽度和高度属性,不会出现?

需要使用Next/Image如何解决

The image is working just fine. Here is the code

<Image
  layout="fixed"
  src="/images/example.jpeg"
  alt="Example image"
  width="140"
  height="140"
 />

But, when I run the website on web.dev, I don't get a topscore in Performance.
The main reason is Image elements do not have explicit width and height

I've studied this, and can tell from here https://web.dev/optimize-cls/?utm_source=lighthouse&utm_medium=lr#images-without-dimensions
That width and height attributes is needed, which don't appear, using next/image

How do I solve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清晰传感 2025-01-25 20:38:07

我面临同样的问题 - 将布局更改为响应式并没有解决问题。
它看起来像是组件缺少支持标签的渲染 - 它实际上应该按照初始灯塔链接中的描述来实现。

I am facing the same issue - changing layout to responsive did not solve the problem.
It rather looks like the component is missing the rendering of the supporting tags - it should actually be implemented as described in the initial lighthouse link.

对你的占有欲 2025-01-25 20:38:07

使用卷曲括号提及大小而不是字符串文字。
使用布局=“响应式”

<Image
  layout="responsive"
  src="/images/example.jpeg"
  alt="Example image"
  width={140}
  height={140}
 />

Use curly braces to mention the size instead of string literals.
Use layout="responsive"

<Image
  layout="responsive"
  src="/images/example.jpeg"
  alt="Example image"
  width={140}
  height={140}
 />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文