我用 gatsby 和 Contentful 建立了一个博客,并在内容帖子中有一个布尔字段来选择特定帖子是否“精选”。如何声明变量 featuresPost 以匹配将特色布尔值设置为 true 的帖子。请注意; 在下面的代码中我放的声明中?????????? 突出我的问题所指的内容/位置。非常感谢您的建议:
...
class BlogIndex extends React.Component {
render() {
const { data } = this.props
const siteTitle = data.site.siteMetadata?.title || `Title`
const posts = data.allContentfulPost.edges
const featuredPost = ???????????
return (
<Layout location={this.props.location}>
<SEO title="FieldPro Blog" keywords={[]} />
<Header />
<FeaturedPost>
{featuredPost.map(({ node }) => {
const title = node.title || node.slug
return (
<div key={node.slug}>
<FeaturedImage>
<Link style={{ boxShadow: `none` }} to={node.slug}>
<Img className="Image" fluid={node.image.fluid} />
</Link>
</FeaturedImage>
<FeaturedText>
<FeaturedTitle>
<Link style={{ boxShadow: `none` }} to={node.slug}>
{title}
</Link>
</FeaturedTitle>
<Labels>
<Caption>{/* node.tags */}</Caption>
</Labels>
</FeaturedText>
</div>
)
})}
</FeaturedPost>
<PostGroup>
{posts.map(({ node }) => {
const title = node.title || node.slug
return (
<Post key={node.slug}>
<PostImage>
<Link style={{ boxShadow: `none` }} to={node.slug}>
<Img className="Image" fluid={node.image.fluid} />
</Link>
</PostImage>
<PostText>
<Title>
<Link style={{ boxShadow: `none` }} to={node.slug}>
{title}
</Link>
</Title>
<Labels>
<Caption>{/* node.tags */}</Caption>
</Labels>
</PostText>
</Post>
)
})}
</PostGroup>
</Layout>
)
}
}
export default BlogIndex
export const pageQuery = graphql`
query {
site {
siteMetadata {
title
}
}
allContentfulPost(sort: { fields: createdAt, order: DESC }) {
edges {
node {
title
slug
featured
image {
fluid {
...GatsbyContentfulFluid
}
}
}
}
}
}
`
...