【发布时间】:2020-01-30 22:51:26
【问题描述】:
我是 gatsby 的新手,并尝试使用不同的模板以编程方式创建页面,但我正在为此苦苦挣扎。
这是我的 gatsby 节点文件,它可以很好地创建团队成员页面,但是当我想使用位于同一文件夹 /templates 的新模板创建博客文章时出现问题
const path = require(`path`)
const { createFilePath } = require(`gatsby-source-filesystem`)
exports.onCreateNode = ({ node, getNode, actions }) => {
const { createNodeField } = actions
if (node.internal.type === `MarkdownRemark`) {
const slug = createFilePath({ node, getNode, basePath: `pages` })
createNodeField({
node,
name: `slug`,
value: slug,
})
}
}
exports.createPages = async ({ graphql, actions }) => {
const { createPage } = actions
const result = await graphql(`
query {
allMarkdownRemark {
edges {
node {
fields {
slug
}
}
}
}
}
`)
result.data.allMarkdownRemark.edges.forEach(({ node }) => {
createPage({
path: node.fields.slug,
component: path.resolve(`./src/templates/member-info.js`),
context: {
slug: node.fields.slug,
},
})
})
}
我尝试了不同的方法,但无法找到解决方案。 谢谢!
【问题讨论】:
标签: javascript graphql gatsby