【发布时间】:2019-06-30 06:54:04
【问题描述】:
GatsbyJS docs 给出这个例子,使用 GraphQL 通过相对路径访问文件:
export const query = graphql`
query {
fileName: file(relativePath: { eq: "images/myimage.jpg" }) {
childImageSharp {
fluid(maxWidth: 400, maxHeight: 250) {
...GatsbyImageSharpFluid
}
}
}
}
`
我就是不能让这个工作,我不知道为什么。我尝试了各种不同的语法,但查询总是为文件名返回 null。这是我最近在 GraphiQL 中的尝试:
{
fileName: file(relativePath: { eq: "./html.js" }) {
id
}
}
我错过了什么?如何通过相对路径访问文件?
阅读答案后编辑:
在我的gatsby-config.js 中有几个路径设置为可查询:
{
resolve: `gatsby-source-filesystem`,
options: {
name: `images`,
path: `${__dirname}/src/images/`
}
},
{
resolve: `gatsby-source-filesystem`,
options: {
path: `${__dirname}/content/posts/`,
name: "posts"
}
},
....
当我查询pic.jpg(而不是images/pic.jpg)时,盖茨比怎么知道我想要images/pic.jpg而不是posts/pic.jpg?这是如何唯一定义路径的?
【问题讨论】: