我在盖茨比有这个页面:
import React from 'react'
import Link from 'gatsby-link'
import IntroPartial from '../partials/themes/intro'
export default class ThemeTemplate extends React.Component {
render(){
const theme = this.props.pathContext.theme
console.dir(this.data)
return (
<div>
<h1>{theme.name}</h1>
<IntroPartial theme={theme} />
</div>
)
}
}
export const pageQuery = graphql`
query ThemeQuery($theme: String){
allMarkdownRemark(
filter: { frontmatter: { themes: { in: [$theme] } } }
){
edges{
node{
frontmatter{
title
themes
}
html
}
}
}
}
`
Run Code Online (Sandbox Code Playgroud)
假设我提供了一个选项,这个查询在GraphQL测试器中工作正常$theme.我如何提供价值$theme?我想把它设置为this.props.pathContext.theme.slug.
文档似乎暗示某些变量应该可以正常工作,但我不确定如何添加自己的变量.
小智 23
传递给graphql的变量来自createPage.它通常在你的gatsby-node文件中调用.您将经常在示例中看到使用的路径,如$ path,因为它是必需的.
为了包含您自己的附加变量以传递给graphql调用,您需要将它们添加到上下文中.稍微修改文档中的示例:
createPage({
path: `/my-sweet-new-page/`,
component: path.resolve(`./src/templates/my-sweet-new-page.js`),
// The context is passed as props to the component as well
// as into the component's GraphQL query.
context: {
theme: `name of your theme`,
},
})
Run Code Online (Sandbox Code Playgroud)
然后,您可以在示例中使用查询中的$ theme.在上面的代码中设置$ theme将在createPages部分(参见示例)中完成,因为您将可以访问所有数据.
| 归档时间: |
|
| 查看次数: |
3644 次 |
| 最近记录: |