【问题标题】:Trying to Display Additional Image Thumbnails Below Main Featured Product Image尝试在主要特色产品图片下方显示其他图片缩略图
【发布时间】:2019-07-11 07:01:10
【问题描述】:

无法从 Moltin API 获取文件以显示在主产品图片下

使用这个 repo:https://github.com/moltin/gatsby-demo-store 并更新 @moltin/gatsby-source-moltin 到 1.3.1 我已经尝试扩展产品 然后按照 Moltin API 文档包含关系的节点模式 在新组件中引用它,但不会渲染其他图像。

查看了其他实现,即 Gatsby Store 示例,它使用 缩略图,可以得到一个微小的可点击紫色细条渲染,但没有图像。该商店使用 Shopify 和 localFile 存储进行图像渲染,因此此用例不适用。

```
import React, { useContext, useEffect, useReducer, useState } from 'react'
import { graphql, withPrefix } from 'gatsby'

import SEO from '../components/SEO'
import Photo from '../components/Photo'
import Badge from '../components/Badge'
import AddToCart from '../components/AddToCart'
import { Shopkit } from '../shopkit'
import Img from 'gatsby-image'


  const {
    meta: { display_price }
  } = product

  return (
    <React.Fragment>
      <SEO
        type="product"
        title={product.meta_title || product.name}
        description={product.meta_description || product.description}
        image={withPrefix(product.mainImage.childImageSharp.fixed.src)}
      />

      <div className="flex flex-wrap md:bg-grey-light">
        <div className="py-2 md:py-5 md:px-5 w-full lg:w-1/2">
          <div className="sticky pin-t">
            <Photo
              src={product.mainImage}
              alt={product.main_image_alt_text || product.name}
            />
           <Img 
             src={product.relationships.files.data.id}
             alt=""
           />  
          </div>
        </div>
......
.....
....
    </React.Fragment>
  )
}

export const query = graphql`
  query($id: String!) {
    product: moltinProduct(id: { eq: $id }) {
      id
      slug
      name
      description
      sku
      mainImage {
        childImageSharp {
          fixed(width: 560) {
            ...GatsbyImageSharpFixed
          }
        }
        publicURL
      }
      meta {
        display_price {
          without_tax {
            formatted
          }
        }
      }
      manage_stock
      meta_title
      meta_description
      on_sale
      bulb
      bulb_qty
      material
      finish
      max_watt
      relationships {
             main_image {
              data {
                id
              }
            }
        files {
          data {
            type
            id
          }
    }
  }
`

export default ProductPage
```

希望在主要产品图片下方显示小缩略图。安慰 使用 Img 组件时的错误消息:未捕获的 TypeError:无法读取 未定义的属性“src”。

【问题讨论】:

    标签: reactjs gatsby moltin


    【解决方案1】:

    gatsby-source-moltinimages 字段添加到产品架构中,您可以像这样查询:

    images {
      childImageSharp {
        fluid(maxWidth: 300) {
          ...GatsbyImageSharpFluid
        }
      }
    }
    

    我在CodeSandbox上整理了一个例子。

    【讨论】:

    • 感谢您回复我,我刚刚尝试了开发中的查询并收到以下错误:Cannot query field "nodes" on type "MoltinProductConnection"
    • 根据项目中 Gatsby 的版本,您可能仍需要在查询中包含边。这个要求在最新版本的 Gatsby 中被删除了,就像我的例子一样。
    猜你喜欢
    • 2011-10-16
    • 1970-01-01
    • 2015-11-23
    • 2016-03-27
    • 2016-11-21
    • 1970-01-01
    • 2016-06-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多