【发布时间】:2019-10-11 19:57:31
【问题描述】:
如何在<div> 中以<h2>$category</h2> 的身份访问和使用$category?
import React from "react"
import { graphql } from "gatsby"
import Layout from "../components/layout"
export default ({ data }) => {
const posts = data.posts
return (
<Layout>
<div>
<h1>List of Posts</h1>
</div>
</Layout>
)
}
export const query = graphql`
query($category: String!) {
posts( { category: { name: $category} }) {
summary
}
}
`
请注意,在我的例子中,GraphQL 返回一个对象列表(而不是一个对象)。我可以从列表中的第一项中读取它,但想知道是否有更好的方法。同样在某些情况下,列表可能是空的,所以这不起作用。
【问题讨论】: