我知道这个问题听起来有点复杂,但事实并非如此。
我会尽量用一种简单的方式来解释:
-
我正在为我的盖茨比网站的主页制作一个博客文章列表。
-
列表由“框”组成,链接到帖子的每个框都有一个背景图像。
-
我将这个背景图像传递给一个样式化组件,它从
支柱
我在main<div>元素中插入的值(您将看到下面的示例)。
-
它工作得很好
,直到我尝试使用src下的本地文件夹中的图像(而不是使用简单地放置在frontmatter上的联机链接)。
以下是我过去的工作:
我把图片的网址放在降价文件的最前面:
---
slug: "/post/my-post"
[...]
imghero: "https:/path/to/image-online.jpg"
---
然后用graphql查询:
const LISTING_QUERY = graphql`
query BlogPostListing {
allMarkdownRemark(limit: 10, sort: {
order: DESC,
fields: [frontmatter___date]
}) {
edges {
node {
excerpt
frontmatter {
title
slug
date(formatString: "MMMM DD, YYYY")
imghero
}
}
}
}
}
`
之后我插入
{node.frontmatter.imghero}
在主沙发上的一个道具上:
const Listing = () => (
<StaticQuery
query={LISTING_QUERY}
render={({allMarkdownRemark}) => (
allMarkdownRemark.edges.map(({node}) => (
<Article img_background={node.frontmatter.imghero} key={node.frontmatter.slug}>
[... etc ...]
</Article>
))
)}
/>
)
export default Listing
最后我叫它
img U背景
样式化组件中的道具:
const Article = styled.article`
background-image: url(${props => props.img_background};);
[... etc ...]
`
这种方法有效。
现在,我想从我的“图像”文件夹,而不是从一个随机的网址获得图像。
-
我安装了
盖茨比评论图片
-
在gatsby-config.js上设置,并在frontmatter上设置一些图像的路径。
-
测试一切
http://localhost:8000/___graphql
(和工作)
-
通过图形ql插入附加查询:
[...]
frontmatter {
date(formatString: "DD MMMM, YYYY")
title
imghero
hero {
childImageSharp{
fluid(maxWidth: 630) {
...GatsbyImageSharpFluid
}
}
}
[...]
-
我使用新路径修改组件上的节点:
<Article img_background={node.frontmatter.hero.childImageSharp.fluid} [...]>
[...]
</Article>
-
盖茨比的发展汇编很好。
但是我的主页是完全白色的。
浏览器的控制台上写着
node.frontmatter.hero为“空”
.
我不知道还能做什么。
谢谢你的帮助。