Gatsby.js网站上的图像不会显示在GitHub页面上



我在获取要在github页面上显示的图像时遇到问题。我使用的是基于存储库的页面。我在JSX中添加了一个简单的img标记,并尝试使用Gatsby文档中描述的方法。我觉得我错过了一些显而易见的东西。

这是代码

import React, { useContext } from 'react';
import AnchorLink from 'react-anchor-link-smooth-scroll';
import { ThemeContext } from 'providers/ThemeProvider';
import { Header } from 'components/theme';
import { Container, Button } from 'components/common';
import dev from 'assets/illustrations/dev.svg';
import { Wrapper, IntroWrapper, Details, Thumbnail } from './styles';
import { withPrefix } from 'gatsby'
import HeadShotPlaceHolder from 'assets/images/HeadShotPlaceHolder.jpeg'
export const Intro = () => {
const { theme } = useContext(ThemeContext);
console.log(HeadShotPlaceHolder)
return (
<Wrapper>
<Header />
<IntroWrapper as={Container}>
<Details theme={theme}>
{/* <h1>Hi There!</h1> */}
<h1>Pamela Welch</h1>
{/* <h4>I’m John and I’m a JAMStack engineer!</h4> */}
<h4>A proven professional with extensive experience in all facets of communication and marketing.</h4>
<Button as={AnchorLink} href="#contact">
Hire me
</Button>
</Details>
<Thumbnail>

{/* This is where the image tag giving me the problem is */}
<img src={ withPrefix(HeadShotPlaceHolder) } alt="I’m John and I’m a JAMStack engineer!" />
</Thumbnail>
</IntroWrapper>
</Wrapper>
);
};

这是的结果

图像链接中断

您的代码看起来不错,您的图像带有前缀路径。但是,为了使其有效,您需要在deploy命令中运行以下代码段:

{
"scripts": {
"deploy": "gatsby build --prefix-paths && gh-pages -d public"
}
}

注意--prefix-paths标志。

有关Gatsby如何在GitHub页面中工作的更多信息。

最新更新