首页 > 解决方案 > Gatsby Image TypeError:无法读取未定义的属性“footerHeart”

问题描述

我试图在 gatsby 应用程序中显示一个小图像,但是,我无法在这里找出错误。

代码

import React from "react"
import Img from "gatsby-image"
import { graphql } from "gatsby"

const Footer = ({ data }) => (
  <div>
    <h1>Hello gatsby-image</h1>
    <Img fluid={data.footerHeart.file.childImageSharp.fluid} alt="footer" />
  </div>
)

export default Footer

export const query = graphql`
  query {
    footerHeart: file(relativePath: { eq: "love.png" }) {
      childImageSharp {
        fluid(maxWidth: 40) {
          ...GatsbyImageSharpFluid
        }
      }
    }
  }
`

错误 错误

标签: graphqlgatsbygatsby-image

解决方案


您已经为您的file字段定义了一个入口点所以使用该入口点作为直接路径,就像这样 <Img fluid={data.footerHeart.childImageSharp.fluid} alt="footer" />


推荐阅读