- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在网站上没有找到这个,但我确实找到了 open bug on Github在撰写本文时,唯一的解决方案是使用 GatsbyImage
。学习将 Gatsby 项目从 2 转换为 3 我已经安装了 gatsby-plugin-image并且正在转换一个在 Hero 组件中使用不变图像的组件,根据文档 StaticImage
应该可以工作。
旧组件:
import React from 'react'
import { graphql, useStaticQuery } from 'gatsby'
import Image from 'gatsby-image'
const query = graphql`
{
person: file(relativePath: {eq: "person.png"}) {
childImageSharp {
fluid {
...GatsbyImageSharpFluid
}
}
}
}
`
const Hero = ({ showPerson }) => {
const { person } = useStaticQuery(query)
return (
<header className="hero">
{showPerson && <Image fluid={person.childImageSharp.fluid} className="hero-person" />}
</header>
)
}
export default Hero
新组件:
import React from 'react'
import { StaticImage } from 'gatsby-plugin-image'
import personImage from '../assets/person.png'
const Hero = ({ showPerson }) => {
console.log(personImage)
return (
<header className="hero">
{showPerson && <StaticImage src={personImage} className="hero-person" alt="person image" />}
</header>
)
}
export default Hero
当我记录我得到的 Assets 时(我的文件路径没有问题):
Hero.js:7 /static/person-c7035ca6b9544d80f8f0626ea3e22ace.png
但是日志呈现:
react_devtools_backend.js:2430 No data found for image "undefined"
Image not loaded /static/person-c7035ca6b9544d80f8f0626ea3e22ace.png
在终端我得到:
"gatsby-plugin-image" threw an error while running the preprocessSource lifecycle:
Cannot read property 'startsWith' of undefined
使用 Gatsby 图像 StaticImage
有没有一种方法可以在不使用 GatsbyImage
的情况下渲染一个在组件中不会改变的图像?
最佳答案
从docs about the new Gatsby Plugin Image可以看出:
Restrictions on using
StaticImage
The images are loaded and processed at build time, so there arerestrictions on how you pass props to the component. The values needto be statically-analyzed at build time, which means you can’t passthem as props from outside the component, or use the results offunction calls, for example. You can either use static values, orvariables within the component’s local scope. See the followingexamples:
所以,<StaticImage>
组件无法处理 Prop 或函数调用来接收图像。在您的情况下,这应该有效:
import React from 'react'
import { StaticImage } from 'gatsby-plugin-image'
const Hero = ({ showPerson }) => {
return (
<header className="hero">
{showPerson && <StaticImage src={`../assets/person.png`} className="hero-person" alt="person image" />}
</header>
)
}
export default Hero
由于您的 v2
相似方法,我建议使用 <GatsbyImage>
而不是 <StaticImage>
, 检查它是否符合您的要求。
对于迁移问题,Gatsby has developed a codemod处理所有 GraphQL 查询和“旧”gatsby-image
s,更改所需的查询和组件。安装插件后,只需运行:
npx gatsby-codemods gatsby-plugin-image
这样,问题应该就解决了。如果没有,您可以在以下位置跟踪类似的堆栈跟踪:
看来这个问题与3.2.0-next.0
有关版本,因此另一种选择是尝试降级(或升级,如果可能)。
关于javascript - Gatsby 插件图像无法读取属性 'startsWith',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66644333/
我正在尝试将图像的路径作为 Prop 传递给组件 注意:组件和索引页都可以通过相同的路径访问图像。当我将路径作为 Prop 传递时它不起作用 在下面的代码中,我尝试使用 GatbsyImage 和 S
我在 gatsby 中的主要字体是在 index.scss 文件夹中定义的,就像这样在 body 标签中。 body { font-family: "Trebuchet MS", "Helveti
我一直在遵循以下步骤:https://github.com/strapi/gatsby-source-strapi 由于 image >> publicURL 也不起作用,我重新安装了最新版本的 ga
语境 Gatsby , headless CMS 奇迹,旁边是 gatsby-plugin-advanced-sitemap 使生成一个强大的 sitemap.xml文件轻而易举。 该插件支持通过 s
目前,我有两个使用 gatsby.js 开发的站点部署到 example.com 和 blog.example.com。我想创建一个 blog.example.com 的子目录到 example.co
我一直在学习本教程 https://www.gatsbyjs.org/packages/gatsby-image/#art-directing-multiple-images但普通人不可能写出 50
我正在使用 Gatsby 的 createResolver API 将 markdown 转换为 html。它在顶层数据上运行良好。但是,我无法让它在嵌套更深的数组上工作。 这是有效的: functi
当我使用 Gatsby (V2) 路由到新页面时,我正在尝试传递数据 我希望然后能够检索该页面上的数据。这可能吗?我已经研究了很多但没有运气所以我觉得我一定错过了什么......谢谢 最佳答案 如果你
默认情况下,我的 Gatsby 网址类似于 2018-09-06-hexagon 有什么办法可以让他们变成/blog/2018/09/06/hexagon ? 这是我的 gatsby-node.js
我有一个 json 文件和一个包含图像的文件夹 plugins/ └── gatsby-source-cars/ ├── images/ ├── cars.json └── g
在生产环境中运行 gatsby。 16. 在开发机器上工作。 来自服务器的错误: success write out redirect data - 0.002s success Build mani
当我想安装 gatsby starter 时,我在终端中遇到了这些错误。 任何人都知道如何解决它? [4/4] 🔨 Building fresh packages... [6/13] ⠁ shar
我正在创建一个名为“new-app”的项目。成功安装 gatsby 后,我尝试运行命令“gatsby new my-app”。我总是收到一个错误,其中指出: ERROR eating new site
我无法使用 gatsby cli 克隆 gatsby-starter-default.git,因为它使用的“git”url 被我们的防火墙规则阻止 我也尝试将以下内容添加到 git config 但仍
我正在创建一个名为“new-app”的项目。成功安装 gatsby 后,我尝试运行命令“gatsby new my-app”。我总是收到一个错误,其中指出: ERROR eating new site
我按照健全性文档创建了一个 internalLink 类型,并根据有关将 internalLinks 与 graphql api 一起使用的提示,我将其创建为单独的类型,如下所示: export de
我创建了由 Gatsby Cloud(个人试用计划)托管并使用 Contentful 的 Gatsby.js 网站。虽然我没有设置 Robot.txt,但生产站点自动在 HTTP header 处添加
最令人沮丧的部分是我之前有这个工作然后不知何故破坏了它,但我正在使用 gatsby-plugin-sharp 和 gatsby-plugin-image 将照片添加到我的主页并看到这个错误: Gats
我创建了由 Gatsby Cloud(个人试用计划)托管并使用 Contentful 的 Gatsby.js 网站。虽然我没有设置 Robot.txt,但生产站点自动在 HTTP header 处添加
最令人沮丧的部分是我之前有这个工作然后不知何故破坏了它,但我正在使用 gatsby-plugin-sharp 和 gatsby-plugin-image 将照片添加到我的主页并看到这个错误: Gats
我是一名优秀的程序员,十分优秀!