【发布时间】:2018-06-23 15:40:16
【问题描述】:
我的工作 index.js 和 post.js 中有 2 个文件 index.js 文件的 graphql 查询如下。
export const query = graphql`
query IndexPageQuery($Limit: Int) {
allMarkdownRemark(sort: {fields: [frontmatter___date], order: DESC}, limit: $Limit) {
totalCount`}}
post.js 的返回值如下:
return(
<div className={`all-posts ${this.props.featured ? 'featured' : ''}`}>
<h3 className="sidebar-heading"> More Posts </h3>
{this.props.posts.map(({ node }) => (
<div className="post" key={node.id}>
<Link
to={node.fields.slug}
css={{ textDecoration: `none`, color: `inherit` }}
>
<div>
<p>
{node.frontmatter.title}{" "}
</p>
<span>— {node.frontmatter.date}</span>
</div>
</Link>
</div>
))}
现在我无法将值从 post.js 传递给变量 Limit 到 index.js 有什么解决办法吗??
【问题讨论】:
-
如果您使用 webpack,请将查询变量设置为全局,例如
global.query = query;希望对您有所帮助 -
@JagjeetSingh 不;实际上,我在问如何在 post.js 中设置变量的值,以便它在 index.js 的查询中呈现
-
-
您可以在 post.js 文件中使用 import,例如
import index from './index.js'并使用index.query
标签: javascript graphql graphql-js