1 回答

TA貢獻(xiàn)1796條經(jīng)驗(yàn) 獲得超7個贊
問題是您用第二個函數(shù)聲明覆蓋了第一個函數(shù)聲明。有點(diǎn)像這樣:
var a = "hello"
a = "world"
createPage相反,您應(yīng)該在單個函數(shù)中執(zhí)行所有查詢并調(diào)用要創(chuàng)建的所有頁面,如下所示:
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])
};
添加回答
舉報(bào)