这是我目前代码的一部分
const posts = result.data.allCockpitHello.edges
const date = new Intl.DateTimeFormat("default", {
month: "short",
day: "2-digit",
year: "numeric",
}).format(posts.node.cockpitCreated,);
posts.forEach((post, index) => {
const previous = index === posts.length - 1 ? null : posts[index + 1].node
const next = index === 0 ? null : posts[index - 1].node
createPage({
path: `/blog/${post.node.Name.value}`,
component: path.resolve(`./src/components/single.js`),
context: {
slug: post.node.Name.value,
date,
previous,
next,
},
})
})
我想知道如何将date
作为道具传递给_single.js_或任何其他可以将值传递给其他方式。
毕竟@ Hypothesis-github,您是否使用createPage
api将数据注入单个组件中? 还是那个单身实际上是模板? 而您可能想要的是这样的:
const posts = result.data.allCockpitHello.edges
const date = new Intl.DateTimeFormat("default", {
month: "short",
day: "2-digit",
year: "numeric",
}).format(posts.node.cockpitCreated,);
posts.forEach((post, index) => {
const previous = index === posts.length - 1 ? null : posts[index + 1].node
const next = index === 0 ? null : posts[index - 1].node
createPage({
path: `/blog/${post.node.Name.value}`,
component: path.resolve(`./src/components/single.js`),
context: {
slug: post.node.Name.value,
cockpidate:date,
previousitem:previous,
nextitem:next,
},
})
})
并在single.js
:
import React from "React"
import {Link} from "gatsby"
const Single=props=>{
const {pageContext}= props
const {slug,cockpidate,nextitem,previousitem}= pageContext
return (
<div>
<h3>{slug}</h3>
<hr/>
<h5>created at {cockpidate}</h5>
<Link to={nextitem}/>next item</Link>
<Link to={previousitem}/>previousitem</Link>
<div>
)
}
export default Single
随时提供反馈
最有用的评论
毕竟@ Hypothesis-github,您是否使用
createPage
api将数据注入单个组件中? 还是那个单身实际上是模板? 而您可能想要的是这样的:并在
single.js
:随时提供反馈