我正在基于一些数据创建页面。
我正在尝试按Gatsby create pages from data
进行查询我从gatsby-node.js文件中传递数据,然后在模板中进行查询:
gatsby-node.js
exports.createPages = ({ actions }) => {
const data = require("./src/data/data.json")
const { createPage } = actions
data.forEach(data => {
createPage({
path: `/${data.slug}/`,
component: require.resolve('./src/templates/listing-template.js'),
context:{data,
slug: data.slug}, // slug is the folder name of images, same as the path slug
})
})
}
如以上链接所述:
我在下面所做的正是:奇怪的是,当我从上下文中提取文件时,该文件显示该文件为未使用,
listing-template.js
import React from "react"
import Listing2 from "../components/listing2"
import Slick from "../components/slickgallery"
export default ({ pageContext: { data, slug } }) => (
<div>
<p>{data.name}</p>
<p>{data.slug}</p>
<Slick data = {query.allFile}/>
<Listing2 listing = {data} />
</div>
)
export const { query } =
graphql`
query QueryImages($slug: String!) {
allFile(
sort: { fields: name, order: DESC }
filter: { relativeDirectory: { eq: $slug } }
) {
edges {
node {
id
name
childImageSharp {
fluid(maxWidth: 350, maxHeight: 250) {
...GatsbyImageSharpFluid
}
}
}
}
}
}
`
问题是查询不返回任何内容。 错误是:allFile未定义