【发布时间】:2020-09-05 21:23:51
【问题描述】:
我认为此时我已经查看了太长时间并且可能没有看到明显的东西,但我无法弄清楚为什么我的查询没有正确获取背景图像数据而是显示为空。我正在使用 package.json 中加载的"gatsby-background-image": "^1.1.1"。我在我的 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;
我错过了什么?
【问题讨论】:
标签: reactjs graphql gatsby gatsby-image