【发布时间】:2021-05-07 14:42:43
【问题描述】:
我正在使用 GraphCMS 作为我的 GatsbyJS 网站的 CMS,我想查询特定的图像文件,然后我可以在 React 组件中使用它。
使用 localhost:8000___grapql 时,我可以使用此路径找到我的所有资产:
{
discord: allGraphCmsAsset(filter: {fileName: {eq: "discord_community.png"}}) {
edges {
node {
localFile {
childImageSharp {
fluid(maxWidth: 600) {
...GatsbyImageSharpFluid
}
}
}
}
}
}
}
在我名为 community.tsx 的 React 组件文件中,我试图渲染查询中定义的不和谐图像,但我似乎无法让它工作。
import React from "react"
import { graphql } from "gatsby"
import Img from "gatsby-image"
import styled from "styled-components"
export default function CommunityPage({ allGraphCmsAsset }) {
return (
<Wrapper>
<Img
fluid={allGraphCmsAsset.discord.localFile.childImageSharp.fluid}
fadeIn={false}
/>
</Wrapper>
)
}
export const imageQuery = graphql`
{
discord: allGraphCmsAsset(filter: {fileName: {eq: "discord_community.png"}}) {
edges {
node {
localFile {
childImageSharp {
fluid(maxWidth: 600) {
...GatsbyImageSharpFluid
}
}
}
}
}
}
}`
const Wrapper = styled.div``
我应该在当前所说的大括号中输入什么?:
fluid={allGraphCmsAsset.discord.localFile.childImageSharp.fluid}
【问题讨论】:
-
签入
localhost:8000___grapql,如果您可以在allGraphCmsAsset上使用一些过滤器,例如allGraphCmsAsset(where:...) { -
酷,这似乎是等式的一部分。然后如何使用该查询将我的图像放入我的反应组件中?
-
在操场上测试它并在
pageQuery中使用? -
我在原帖中更新了我的问题。感谢您的帮助。
-
你必须使用
pageQuery常量名!! ...您可以在返回之前在组件中console.log( allGraphCmsAsset )...检查它的结构(非常基本的js问题)
标签: graphql gatsby gatsby-image