【发布时间】:2022-01-19 22:41:15
【问题描述】:
我有我想要多次查询并获取不同尺寸的英雄图像,以便我可以在不同的设备尺寸中使用它。
我的英雄形象在
src/images/hero.png
这是查询代码:
export const mobileHeroImage = graphql`
fragment mobileHeroImage on File {
childImageSharp {
fluid(maxWidth: 375, maxHeight: 400) {
...GatsbyImageSharpFluid
}
}
}
`;
export const query = graphql`
query {
mobileHeroImage: file(relativePath: { eq: "hero.png" }) {
...mobileHeroImage
}
}
`;
这是我在 index.js 中的组件代码:
const IndexPage = ({ data }) => {
// First console log
console.log(data);
// Second console log
console.log(getImage(data.mobileHeroImage));
第一个控制台日志记录带有图像对象的对象:
即使data 内部有mobileHeroImage 对象,第二个控制台日志也会记录未定义
多维图像将作为数组传递,如下所示:
export function MyImage({ data }) {
const images = withArtDirection(getImage(data.mobileHeroImage), [
// I would add more sizes here for different screen sizes
{
media: "(max-width: 1024px)",
image: getImage(data.smallImage),
},
])
return <GatsbyImage image={images} />
}
【问题讨论】:
标签: gatsby gatsby-image