【问题标题】:Why GraphQL doesn't work on GatsbyJS layouts?为什么 GraphQL 不适用于 GatsbyJS 布局?
【发布时间】:2018-09-05 01:57:34
【问题描述】:

我想查询我的布局中的一些数据,但 {console.log(data)} 返回 undefined。这意味着我的查询不起作用。

我的 layouts/index.js 文件:

import React from "react";
import Header from '../components/Header'
import Footer from '../components/Footer'
import Slider from "../components/Slider";
import {graphql} from "gatsby";

class DefaultLayout extends React.Component {
    render() {

        const {data} = this.props

        return (
            <div className='container-fluid'>
                <Header/>

                {console.log(data)}
                {console.log(data)}
                {console.log(data)}

                <div className="row rtl">
                    <div className="col routeContainer">
                        {this.props.children}
                    </div>
                    <div className="col slider"><Slider/></div>
                </div>
                <Footer/>
            </div>
        )}}
export default DefaultLayout
export const pageQuery = graphql`
      query {
         site {
          siteMetadata {
            title
            subtitle
          }
        }
      }
    `

为什么会这样?!

接下来使用 react 16.3.2 和 gatsby。

【问题讨论】:

    标签: layout graphql gatsby


    【解决方案1】:

    您需要使用静态查询。你的布局文件现在只是一个普通的 React 组件。

    https://next.gatsbyjs.org/docs/migrating-from-v1-to-v2/#remove-or-refactor-layout-components

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-21
      • 2014-04-28
      • 2020-03-12
      • 2018-07-12
      • 2015-05-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多