您可以在构建时动态创建页面,为此需要向
gatsby-node.js
文件下面是一个简单的片段。
const path = require('path')
exports.createPages = ({graphql, boundActionCreators}) => {
const {createPage} = boundActionCreators
return new Promise((resolve, reject) => {
const storeTemplate = path.resolve('src/templates/store.js')
resolve(
graphql(`
{
allContentfulStore (limit:100) {
edges {
node {
id
name
slug
}
}
}
}
`).then((result) => {
if (result.errors) {
reject(result.errors)
}
result.data.allContentfulStore.edges.forEach((edge) => {
createPage ({
path: edge.node.slug,
component: storeTemplate,
context: {
slug: edge.node.slug
}
})
})
return
})
)
})
}
这个
createPages
导出的是一个Gatsby节点API函数,您可以在文档中找到完整的列表
here
.
用于查询
allContentfulStore
之所以这样叫是因为您的contentType名称是
store
gatsby查询将是allContentful{ContentTypeName}。
最后,我创建了一个youtube视频系列,解释如何创建一个内容丰富的盖茨比网站。你可以找到它
here
我希望这能回答你的问题。
干杯
哈立德