【发布时间】:2019-07-05 08:31:19
【问题描述】:
我的 Gatsby 网站需要 2 个博客模板:
- 故事模板.js
- products.template.js
我为我的故事运行了故事模板,但我不确定如何调整和更改 gatsby-node + products.template.js 中的现有代码,以便为我的产品制作第二个(不同的)模板。
我已经尝试了所有解决方案和过去的问题,但没有运气。
我在 gatsby-node.js 中的代码:
const path = require('path');
exports.createPages = ({actions, graphql}) => {
const { createPage } = actions
const postTemplate = path.resolve('src/components/stories-template.js');
return graphql(`
{
allMarkdownRemark {
edges {
node {
html
id
frontmatter {
path
title
author
date
}
}
}
}
}
`).then(res => {
if(res.errors) {
return Promise.reject(res.errors)
}
res.data.allMarkdownRemark.edges.forEach(({ node }) => {
createPage({
path: node.frontmatter.path,
component: postTemplate,
})
})
})
}
我在 stories-template.js 中的代码:
import React from 'react'
import Layout from '../components/layout'
export default function Template({data}) {
const post = data.markdownRemark
return(<Layout>
<div>
<p>Stories</p>
<br />
<p>{post.frontmatter.title}</p>
<div dangerouslySetInnerHTML={{__html: post.html}} />
</div>
</Layout>
)
}
export const postQuery = graphql`
query BlogPostByPath($path: String!) {
markdownRemark(frontmatter: { path: {eq:$path}}){
html
frontmatter{
path
title
date
author
}
}
}
`
这可行,但现在我想在 products-template.js 中为产品创建不同的模板。现在我的产品模板基本上只是从我的故事模板中复制和粘贴的。
我这辈子都想不通。
【问题讨论】:
-
欢迎来到 SO!这涉及几个步骤(创建新模板、设置新内容、进行新查询、创建新页面),您在哪些步骤遇到问题?您尝试了什么,遇到了什么错误?
标签: gatsby