【问题标题】:gatsby-image-background using v3 gatsby-image盖茨比图像背景使用 v3 盖茨比图像
【发布时间】:2021-07-30 08:37:24
【问题描述】:

我正在尝试使 gatsby-background-image 与 v3 的 gatsby-plugin-image 一起使用。我遵循了文档,发现我应该使用 gbimage-bridge。

由于某种原因,它似乎不起作用。在 ide 中测试时,我的查询工作正常。我试图以各种方式更改我的查询和常量,但似乎无法使其工作。

现在它只输出文本 Test 但没有显示背景。

我的代码:

import { graphql, useStaticQuery } from "gatsby"
import { getImage } from "gatsby-plugin-image"
import { BgImage } from "gbimage-bridge"

const GbiBridged = () => {
  const { backgroundImage123 } = useStaticQuery(graphql`
    query {
      backgroundImage123: allWpPage {
        nodes {
          ACFforside {
            heroimg {
              localFile {
                childImageSharp {
                  gatsbyImageData(
                    width: 2000
                    quality: 50
                    placeholder: BLURRED
                    formats: [AUTO, WEBP, AVIF]
                  )
                }
              }
            }
          }
        }
      }
    }
  `)

  const pluginImage = getImage(backgroundImage123)

  return (
        <BgImage image={pluginImage}>Test</BgImage>
  )
}

export default GbiBridged

【问题讨论】:

    标签: javascript reactjs graphql gatsby gatsby-image


    【解决方案1】:

    我认为你的 sn-p 应该是这样的:

    import React from 'react'
    import { graphql, useStaticQuery } from 'gatsby'
    import { getImage, GatsbyImage } from "gatsby-plugin-image"
    
    import { convertToBgImage } from "gbimage-bridge"
    import BackgroundImage from 'gatsby-background-image'
    
    const GbiBridged = () => {
      const { backgroundImage123 } = useStaticQuery(
        graphql`
          query {
            backgroundImage123: allWpPage {
              nodes {
               ACFforside {
                 heroimg {
                   localFile {
                     childImageSharp {
                       gatsbyImageData(
                         width: 2000
                         quality: 50
                         placeholder: BLURRED
                         formats: [AUTO, WEBP, AVIF]
                       )
                     }
                   }
                 }
               }
             }
           }
         }
        `
      )
      const image = getImage(backgroundImage123.nodes[0].ACFforside.heroimg.localFile)
    
      // Use like this:
      const bgImage = convertToBgImage(image)
    
      return (
        <BackgroundImage
          Tag="section"
          // Spread bgImage into BackgroundImage:
          {...bgImage}
          preserveStackingContext
        >
          <div style={{minHeight: 1000, minWidth: 1000}}>
            <GatsbyImage image={image} alt={"testimage"}/>
          </div>
        </BackgroundImage>
      )
    }
    export default GbiBridged
    

    我假设您的查询正在获取正确的节点,否则,请在 localhost:8000/___graphql 操场上进行测试

    【讨论】:

    • 谢谢,费兰!现在页面只是变成空白并给出控制台错误:“警告:道具类型失败:道具imageGatsbyImage中被标记为必需,但它的值是undefined”.....我已经检查了___graphql操场和我的查询似乎正在获取正确的节点。截图:p221.p4.n0.cdn.getcloudapp.com/items/NQuwmBXP/…
    • 我错过了这个:const image = getImage(backgroundImage123.nodes[0].ACFforside.heroimg.localFile) 如果这不起作用(因为我快速输入了结构),请检查您是否将正确的节点传递给 getImage 函数
    猜你喜欢
    • 2019-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-08
    • 2021-02-13
    • 2020-04-20
    • 1970-01-01
    • 2021-09-29
    相关资源
    最近更新 更多