0

我正在构建一个包含多个gatsby-source-filesystem实例的博客。

我正在尝试在页面上使用 gatsby-image 但它只是返回: TypeError: Cannot read property 'fixed' of undefined

我要查询的图像位于 src/images

精神.js

import React from "react"
import { graphql } from "gatsby"
import Img from 'gatsby-image'

import Layout from "../components/layout"
import SEO from "../components/seo"
import Paper from '../components/paper'


const SpiritsPage = ({data}) => (
        <Layout>
            <SEO title="Spirits" />
            <Paper>
                <h1>Spirits</h1>
                <p>This section is still under construction.</p>
                <Img fixed={data.allImageSharp.edges.node.fixed} alt />
            </Paper>
        </Layout>
    )

export const query = graphql`
    query {
        allImageSharp(filter: {fluid: {originalName: {eq: "australia.png"}}}) {
            edges {
                node {
                    fixed {
                        ...GatsbyImageSharpFixed
                    }
                }
            }
        }
    }

`

export default SpiritsPage

盖茨比-config.js

{
            resolve: `gatsby-source-filesystem`,
            options: {
                name: `distilleries`,
                path: `${__dirname}/content/distilleries`,
            },
        },
        {
            resolve: `gatsby-source-filesystem`,
            options: {
                name: `images`,
                path: `${__dirname}/src/images`,
            },
        },
        {
            resolve: `gatsby-source-filesystem`,
            options: {
                name: `posts`,
                path: `${__dirname}/content/posts`
            },
        },
        `gatsby-transformer-sharp`,
        `gatsby-plugin-sharp`,
4

1 回答 1

0

data.alImageSharp.edges是一个数组,所以你不能做data.allImageSharp.edges.node. 相反,您需要做的是从edges数组中获取您想要的项目,然后node.fixed对其进行操作。像下面这样的东西会起作用:data.allImageSharp.edges[0].node.fixed

信用 - goto1

于 2020-02-26T05:49:44.763 回答