盖茨比插件图片 | <StaticImage /> 不显示图片

gatsby-plugin-image | <StaticImage /> Does not display the image

我想优化我的 Gatsby 网站上的所有图像,为此我安装了 gatsby-image-plugin

对于动态图像,我使用的是 GatsbyImage 组件,一切正常,没有问题。

问题是当我想使用 StaticImage 组件 渲染静态图像时。

举个例子:

import laptop from '@images/laptop.png';

如果我导入图像并以这种方式使用它:

<img src={laptop} alt='laptop' />

图像在浏览器上正确显示,但如果我尝试这样做:

import { StaticImage } from 'gatsby-plugin-image';
<StaticImage src={laptop} alt='laptop' />;

图像未显示在浏览器中,我在控制台中收到以下消息:

Image not loaded /static/laptop-5f8db7cd28b221fc1a42d3ecd6baa636.png

并且终端出现这个错误:

Could not find values for the following props at build time: src Image not loaded /static/laptop-5f8db7cd28b221fc1a42d3ecd6baa636.png

我试图将来自互联网的随机图像 src 作为 link 传递,并且该图像显示在浏览器上! 为什么当我使用资产文件夹中的图像时它不起作用?

PS;阅读插件文档我看到有一些限制,例如 你不能传递来自道具的图像,但事实并非如此!我是直接从assets文件夹导入图片。

有线索吗?提前谢谢你。

PS; Reading the plugin documentation I saw that there are some restrictions like you cannot pass images that are coming from the props, but this is not the case! I am importing the image directly from the assets folder.

您正在从资产文件夹中导入图像,但仍将其作为道具传递给 StaticImage。正确的用法如下:

<StaticImage src='@images/laptop.png' alt='laptop' />

根据 Gatsby Image Plugin 文档,src 必须是类型 string。您当前正在传递一个对象 {laptop}。将其更改为带有图像文件路径的字符串,它将显示。