简体   繁体   English

在 Gatsby 模板的查询中引用 GraphQL 变量?

[英]Referencing GraphQL variable used in query in Gatsby template?

I am aggregating all posts that have a certain tag in it's tag list:我正在汇总所有在其标签列表中具有特定标签的帖子:


export const Search = graphql`
  query SearchResults($tag: String = "") {
    allMarkdownRemark(filter: { frontmatter: { tags: { in: [$tag] } } }) {
      nodes {
        frontmatter {
          title
          date
        }
      }
    }
  }
`

The query works great, but I want to also be able to dynamically show the tag that is being queried for.查询效果很好,但我还希望能够动态显示正在查询的标签。 How can I pass this information through?我怎样才能传递这些信息?

For example: Searching results for tag: Java which would be the value inside $tag in the graphql query.例如:标签的搜索结果: Java这将是 graphql 查询中$tag内的值。

I tried to pull from the URL, but it's rendered in node, so I don't have access to the window object and it felt a bit hacky anyway.我试图从 URL 中提取,但它是在节点中呈现的,所以我无法访问 window object 并且感觉有点老套。

I got it.我知道了。 Passing in props.pageContext gives you access to the contextual information passed in via gatsby-node. props.pageContext让您访问通过 gatsby-node 传入的上下文信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM