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])
};
添加回答
舉報