1 回答
TA贡献1796条经验 获得超7个赞
问题是您用第二个函数声明覆盖了第一个函数声明。有点像这样:
var a = "hello"
a = "world"
createPage相反,您应该在单个函数中执行所有查询并调用要创建的所有页面,如下所示:
exports.createPages = ({ graphql, actions }) => {
const { createPage } = actions;
const collections = graphql(`
query {
allFile(filter: {relativeDirectory: {eq: "collections"}}) {
edges {
node {
childMarkdownRemark {
fields {
slug
}
}
}
}
}
}
`).then(result => {
result.data.allFile.edges.forEach(({ node }) => {
createPage({
path: node.childMarkdownRemark.fields.slug,
component: path.resolve('./src/templates/collection.js'),
context: {
slug: node.childMarkdownRemark.fields.slug,
},
});
});
})
const posts = graphql(`
query {
allFile(filter: {relativeDirectory: {eq: "posts"}}) {
edges {
node {
childMarkdownRemark {
fields {
slug
}
}
}
}
}
}
`).then(result => {
result.data.allFile.edges.forEach(({ node }) => {
createPage({
path: node.childMarkdownRemark.fields.slug,
component: path.resolve('./src/templates/post.js'),
context: {
slug: node.childMarkdownRemark.fields.slug,
},
});
});
})
return Promise.all([collections, posts])
};
添加回答
举报