【问题标题】:Referencing GraphQL variable used in query in Gatsby template?引用 Gatsby 模板中查询中使用的 GraphQL 变量?
【发布时间】:2021-11-24 01:45:37
【问题描述】:

我正在汇总标签列表中具有特定标签的所有帖子:


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

查询效果很好,但我还希望能够动态显示正在查询的标签。我该如何传递这些信息?

例如:标签的搜索结果:Java,这将是 graphql 查询中 $tag 内的值。

我试图从 URL 中提取,但它是在节点中呈现的,所以我无法访问窗口对象,而且感觉有点 hacky。

【问题讨论】:

    标签: javascript graphql gatsby


    【解决方案1】:

    我明白了。传入props.pageContext 可让您访问通过 gatsby-node 传入的上下文信息。

    【讨论】:

      猜你喜欢
      • 2019-10-13
      • 2020-11-30
      • 2019-10-25
      • 2021-10-13
      • 1970-01-01
      • 2022-01-21
      • 2019-11-22
      • 2021-07-22
      • 2020-03-23
      相关资源
      最近更新 更多