【发布时间】:2018-08-31 08:53:56
【问题描述】:
所以我用产品建立了一个 Gatsby 博客,它可以以编程方式为每个产品生成页面。我想添加到这个项目中,以便它也为产品类别生成页面。每个类别页面将显示该类别中的所有产品。 内容由 Contentful CMS 提供。
这可能吗?添加第二个布局模板并创建此类页面?
exports.createPages = ({ graphql, boundActionCreators }) => {
const { createPage } = boundActionCreators
return new Promise((resolve, reject) => {
const blogPost = path.resolve('./src/templates/blog-post.js')
const categoryPage = path.resolve('./src/templates/category-post.js')
resolve(
graphql(
`
{
allContentfulBlog(limit: 200) {
edges {
node {
id
categories
mainTitle
mainImg {
id
}
mainText {
childMarkdownRemark {
excerpt
}
}
slug
}
}
}
}
`
)
.then(result => {
if (result.errors) {
console.log(result.errors)
reject(result.errors)
}
result.data.allContentfulBlog.edges.forEach(edge => {
createPage({
path: edge.node.slug,
component: blogPost,
context: {
slug: edge.node.slug,
},
})
})
return
})
.then(result => {
result.forEach(category => {
createPage({
path: '/' + edge.node.category,
component: categoryPage,
context: {
slug: edge.node.category,
},
})
})
})
)
})
}
promise 中的第一个 .then() 方法工作得很好,我的想法是简单地添加第二个 .then(),使用类别页面的模板。
【问题讨论】:
标签: reactjs gatsby contentful