0

我正在尝试通过构建一个演示博客来学习 Gatsby。我有一个存档组件,我在其中查询博客数据,并且我试图将图像作为道具向下传递,但是,当我尝试访问我的文章组件中的道具时,我不断得到一个TypeError: Cannot read property 'childImageSharp' of null.

这是我的代码:

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

// components
import Article from "./article"

const BLOG_QUERY = graphql`
  {
    allMarkdownRemark {
      nodes {
        excerpt
        frontmatter {
          title
          date(formatString: "MMMM DD, YYYY")
          author
          slug
          image {
            childImageSharp {
              fluid(maxWidth: 600) {
                ...GatsbyImageSharpFluid_withWebp_tracedSVG
              }
            }
          }
        }
        timeToRead
      }
    }
  }
`
const Archive = () => {
  const data = useStaticQuery(BLOG_QUERY)
  console.log(data)
  return (
    <div>
      {data.allMarkdownRemark.nodes.map(node => (
        <Article
          key={node.frontmatter.slug}
          title={node.frontmatter.title}
          excerpt={node.excerpt}
          image={node.frontmatter.image.childImageSharp.fluid}
        />
      ))}
    </div>
  )
}

export default Archive

Markdown 标头如下:

---
slug: "/what-is-gatsby"
title: "What Is Gatsby?"
image: "../images/gatsby.png"
author: "Joshua Isaac"
date: "2019-10-23"
---

记录的数据: 在此处输入图像描述

文章组成:

import { React } from 'react'
import Img from 'gatsby-image'

const Article = (props) => {
 return(
  <div>
    <h3>{props.frontmatter.title}</h3>
    <Img fluid={props.image} />
  </div>

 )
}
4

2 回答 2

0

也许它对某人有用:

  1. 安装npm install --save gatsby-source-filesystem

  2. 添加到您的 gatsby-config.js:

    {
      resolve: `gatsby-source-filesystem`,
      options: {
        name: `static`,
        path: `${__dirname}/static/blog/`,
      },
    },
  1. 在 src 旁边创建一个文件夹static

  2. 在降价文件中更改图像的路径

  3. 然后:

import React from "react"
import { useStaticQuery, graphql } from "gatsby"

// components
import Article from "./article"

const BLOG_QUERY = graphql`
  {
    allMarkdownRemark {
      nodes {
        excerpt
        frontmatter {
          title
          date(formatString: "MMMM DD, YYYY")
          author
          slug
          image {
            childImageSharp {
               gatsbyImageData(
                 formats: [AUTO, WEBP, AVIF]
               )
            }
          }
        }
        timeToRead
      }
    }
  }
`
const Archive = () => {
  const data = useStaticQuery(BLOG_QUERY)
  console.log(data)
  return (
    <div>
      {data.allMarkdownRemark.nodes.map(node => (
        <Article
          key={node.frontmatter.slug}
          title={node.frontmatter.title}
          excerpt={node.excerpt}
          image={node.frontmatter.image}
        />
      ))}
    </div>
  )
}

export default Archive
import { React } from 'react'
import { GatsbyImage, getImage } from "gatsby-plugin-image"

const Article = (props) => {

const image = getImage(props.image)

 return(
  <div>
    <h3>{props.title}</h3>
    <GatsbyImage image={image} alt={props.title} />
  </div>

 )
}
  1. 如果它不起作用:gatsby clean
于 2021-04-11T10:03:32.420 回答
0

确保每个 markdown 文件image在 frontmatter 中都有一个属性,其中包含图像的相对路径。

此外,<Article/>看起来您正在尝试访问不存在的属性:props.frontmatter.title.

您只将key, title, excerpt & image道具传递给<Article/>,因此道具frontmatter根本不存在。

// article.js

import { React } from 'react'
import Img from 'gatsby-image'

const Article = (props) => {
 return(
  <div>
    <h3>{props.title}</h3>
    <Img fluid={props.image} />
  </div>
 )
}
于 2019-10-24T13:18:19.573 回答