我想在这一点上,我已经看得太久了,可能没有看到明显的东西,但我不明白为什么我的查询没有正确获取背景图像数据,而是显示为空。我正在使用"gatsby-background-image": "^1.1.1"
加载在 package.json 中。我在我的 gatsby-config.js 中引用我的图像文件,如下所示:
{
resolve: `gatsby-source-filesystem`,
options: {
name: `images`,
path: `${__dirname}/src/images`,
},
},
以及使用它的组件:
import React from 'react';
import { graphql, StaticQuery } from 'gatsby';
import styled from 'styled-components';
import BackgroundImage from 'gatsby-background-image';
const BackgroundSection = () => (
<StaticQuery
query={graphql`
query {
desktop: file(relativePath: { eq: "demo.jpg" }) {
childImageSharp {
fluid(quality: 90, maxWidth: 1920) {
...GatsbyImageSharpFluid_withWebp
}
}
}
}
`}
render={data => {
/* eslint-disable no-console */
console.log(data);
/* eslint-enable no-console */
// Set ImageData.
const imageData = data.desktop.childImageSharp.fluid;
return (
<BackgroundImage
Tag="section"
fluid={imageData}
backgroundColor={`#040e18`}
>
<h2>gatsby-background-image</h2>
</BackgroundImage>
);
}}
/>
);
const StyledBackgroundSection = styled(BackgroundSection)`
width: 100%;
background-position: bottom center;
background-repeat: repeat-y;
background-size: cover;
`;
export default StyledBackgroundSection;
我错过了什么?