Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
413 views
in Technique[技术] by (71.8m points)

graphql - 如何在基于非模板的页面(src / pages的一部分)中将变量传递给GraphQL(How to pass variable to GraphQL in a non-template based page (part of src/pages))

While I'm able to pass variable via pageContext to GraphQL on all template based pages ( which use createPage function), I'm not able to pass/modify the context for pages which are part of src/page.

(虽然我能够通过pageContext将变量传递给所有基于模板的页面(使用createPage函数)上的GraphQL,但是我无法传递/修改属于src / page的页面的上下文。)

Consider this home page : src/pages/index.js

(考虑此主页:src / pages / index.js)

//want to pass dynamic value to $today
const IndexPage = ( {data, pageContext} ) => {
    console.log(pageContext); // prints "isCreatedByStatefulCreatePages: true"

    return (
    <Layout />
)};

export default IndexPage;


export const Query = graphql`
  offers( where : { day: $today})
  {
    ..fields
  }
`;
  ask by Shatiz translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

In order to modify automatically created pages you will need to use the onCreatePage node API.

(为了修改自动创建的页面,您将需要使用onCreatePage节点API。)

You basically recreate (delete/create) pages while modifying the page object.

(您基本上可以在修改页面对象的同时重新创建(删除/创建)页面。)

Just follow instructions in the docs Creating and Modifying Pages

(只需按照docs 创建和修改页面中的说明进行操作)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...