【发布时间】:2019-10-19 14:09:13
【问题描述】:
我有 16 张图像要以网格格式呈现到网站上。
我为此使用了以下插件:
gatsby-imagegatsby-source-filesystemgatsby-plugin-sharpgatsby-transformer-sharp
我阅读了文档,据我所知,它只演示了如何查询单个图像。
例如
import React from "react"
import { graphql } from "gatsby"
import Img from "gatsby-image"
export default ({ data }) => (
<div>
<h1>Hello gatsby-image</h1>
<Img fixed={data.file.childImageSharp.fixed} />
</div>
)
export const query = graphql`
query {
file(relativePath: { eq: "blog/avatars/kyle-mathews.jpeg" }) {
childImageSharp {
# Specify the image processing specifications right in the query.
# Makes it trivial to update as your page's design changes.
fixed(width: 125, height: 125) {
...GatsbyImageSharpFixed
}
}
}
}
`
但是,如果我有 16 张图片,我该怎么做呢?编写 16 个单独的查询似乎相当麻烦,并且将来难以阅读。
我在引用多张图片的文档中看到了这段代码,但我在尝试访问这些图片时遇到了麻烦。
例如
export const squareImage = graphql`
fragment squareImage on File {
childImageSharp {
fluid(maxWidth: 200, maxHeight: 200) {
...GatsbyImageSharpFluid
}
}
}
`
export const query = graphql`
query {
image1: file(relativePath: { eq: "images/image1.jpg" }) {
...squareImage
}
image2: file(relativePath: { eq: "images/image2.jpg" }) {
...squareImage
}
image3: file(relativePath: { eq: "images/image3.jpg" }) {
...squareImage
}
}
`
我的文件夹结构如下:
---package.json
---src
-----图片
--------- 16 张图片
------页面
---------我要在其中渲染16张图片的页面
等等
谢谢。
【问题讨论】:
-
提供的答案没有解释如何使用示例中给出的代码。我还没有弄清楚如何导入image1、image2和image3。任何帮助将不胜感激。
-
Hi @R3N0、“image1:”、“image2:”等是别名,因此您可以将它们称为
data.image1.childImageSharp...等。