Graphql 使用盖茨比插件图像进行灵活反应

Graphql 使用盖茨比插件图像进行灵活反应,graphql,gatsby,react-slick,Graphql,Gatsby,React Slick,我正在尝试使用React slick与盖茨比插件图像,我有这样的页面设置 import React from "react"; import { graphql } from "gatsby" import Slider from "react-slick"; import "slick-carousel/slick/slick.css"; import "slick-carousel/slick/slic

我正在尝试使用React slick与盖茨比插件图像,我有这样的页面设置

import React from "react";
import { graphql } from "gatsby"
import Slider from "react-slick";
import "slick-carousel/slick/slick.css";
import "slick-carousel/slick/slick-theme.css";
import { GatsbyImage } from "gatsby-plugin-image"

const settings = {
  autoPlay: true,
  arrows: false,
  dots: true,
  infinite: true,
  speed: 500,
  slidesToShow: 1,
  slidesToScroll: 1,
};

const ImgSlide = ({ data }) => {
  return (
    <div>
      <Slider {...settings}>
        <div>
          <GatsbyImage fluid={data.image1.childImageSharp.fluid} />
        </div>
        <div>
        <GatsbyImage fluid={data.image2.childImageSharp.fluid} />
        </div>
      </Slider>
    </div>
  );
};

export const pageQuery = graphql`
  query {
    image1: file(relativePath: { eq: "images/icon.png" }) {
      childImageSharp {
        fluid {
          ...GatsbyImageSharpFluid
        }
      }
    }
    image2: file(relativePath: { eq: "images/icon.png" }) {
      childImageSharp {
        fluid {
          ...GatsbyImageSharpFluid
        }
      }
    }
  }
`

export default ImgSlide;

传递图像时,新的
组件的结构本身使用的是
图像
道具
,而不是
流体
。此外,查询需要获取
gatsbyImageData
,而不是
fluid
,您可以在以下页面中看到:


首先在PlayGround中测试查询,因此当我在graphiql中运行查询时,运行相同的查询结构确实会显示查询的文件。image1:和Image2一定有问题,但我不知道如何声明它们,以便可以单独引用它们在返回JSX之前?盖茨比甚至不会构建当我尝试在本地主机中打开时,它会给出错误,并且不会让我传递它。渲染一些伪组件甚至字符串?

module.exports = {
  siteMetadata: {
    title: "Inkd Era",
    description: "Clothing and brand built for tattoo and tattoed culture",
  },
  plugins: [
    "gatsby-plugin-sass",
    "gatsby-plugin-image",
    "gatsby-plugin-react-helmet",
    "gatsby-plugin-sitemap",
    {
      resolve: "gatsby-plugin-manifest",
      options: {
        icon: "src/images/icon.png",
      },
    },
    "gatsby-transformer-remark",
    "gatsby-plugin-sharp",
    "gatsby-transformer-sharp",
    {
      resolve: "gatsby-transformer-remark",
      options: {
        plugins: [
          {
            resolve: "gatsby-remark-images",
            options: {
              maxWidth: 650,
            },
          },
        ],
      },
    },
    {
      resolve: "gatsby-source-filesystem",
      options: {
        name: "images",
        path: `${__dirname}/src/images/`,
      },
      __key: "images",
    },
    {
      resolve: "gatsby-source-filesystem",
      options: {
        name: "pages",
        path: `${__dirname}/src/pages/`,
      },
      __key: "pages",
    },
    {
    resolve: `gatsby-plugin-manifest`,
    options: {
      name: `Inkd Era`,
      short_name: `Inkd era`,
      start_url: `/`,
      background_color: `#000`,
      theme_color: `#fafafa`,
      display: `standalone`,
      icon: `content/assets/gatsby-icon.png`,
    },
  },
  ],
};
import { graphql } from "gatsby"
import { GatsbyImage, getImage } from "gatsby-plugin-image"

function BlogPost({ data }) {
  const image = getImage(data.blogPost.avatar)
  return (
    <section>
      <h2>{data.blogPost.title}</h2>
      <GatsbyImage image={image} alt={data.blogPost.author} />
      <p>{data.blogPost.body}</p>
    </section>
  )
}

export const pageQuery = graphql`
  query {
    blogPost(id: { eq: $Id }) {
      title
      body
      author
      avatar {
        childImageSharp {
          gatsbyImageData(
            width: 200
            placeholder: BLURRED
            formats: [AUTO, WEBP, AVIF]
          )
        }
      }
    }
  }
`
import Img from `gatsby-image`

<Img fluid={data.image1.childImageSharp.fluid} />