非页面组件中的GraphQL查询

时间:2020-09-01 16:29:54

标签: graphql gatsby contentful

我使用的材料

・内容充实 ・盖茨比 ・ Graphql

成功

使用8000_graphql从内容丰富的内容中获取内容,并显示在本地URL(http:// localhost:8000 /)

失败

切换页面URL(http:// localhost:8000 /)到 显示404页的URL(http:// localhost:8000 / blog / ....)。

错误

warn The GraphQL query in the non-page component "C:/Users/taiga/Github/Gatsby-new-development-blog/my-blog/src/templates/blog.js" will not
Exported queries are only executed for Page components. It's possible you're
trying to create pages in your gatsby-node.js and that's failing for some
reason.

If the failing component(s) is a regular component and not intended to be a page
component, you generally want to use a <StaticQuery> (https://gatsbyjs.org/docs/static-query)
instead of exporting a page query.

blog.js

import React from 'react';
import { graphql } from 'gatsby';
import Layout from '../components/layout';
import Nav from '../components/nav';
import SEO from '../components/seo';
import './blog.css';

const BlogTemplate = (props) => {
    return (
        <Layout>
        <SEO title={props.data.contentfulBlog.seoTitle} description={props.data.contentfulBlog.seoDescription} keywords={props.data.contentfulBlog.seoKeywords} />
        <Nav />
        <div className='blog__header'>
            <div className='blog__hero' style={{backgroundImage:  `url(${props.data.contentfulBlog.featuredImage.fluid.src})`}}></div>
            <div className='blog__info'>
                <h1 className='blog__title'>{props.data.contentfulBlog.title}</h1>
            </div>
        </div>
        <div className='blog__wrapper'>
            <div className='blog__content'>
                <div dangerouslySetInnerHTML={
                    {__html: `${props.data.contentfulBlog.content.childMarkdownRemark.html}`}
                } />
            </div>
        </div>
        </Layout>
    )
}

export default BlogTemplate;


export const query = graphql`
 query BlogTemplate($id: String!) {
   contentfulBlog(id: {eq: $id}) {
     title
     id
     slug
     content {
       childMarkdownRemark {
         html
       }
     }
     seoTitle
     seoDescription
     seoAuthor
     seoKeywords
     seoImage {
       fluid(maxWidth: 1200, quality: 100) {
         ...GatsbyContentfulFluid
         src
       }
     }
     featuredImage {
       fluid(maxWidth: 1200, quality: 100) {
         ...GatsbyContentfulFluid
         src
       }
     }
   }
 }
`

0 个答案:

没有答案