【问题标题】:Issues with importing images for ReactJS App为 ReactJS 应用程序导入图像的问题
【发布时间】:2021-06-15 05:32:01
【问题描述】:

使用 create-react-app 和 tailwindcss 创建了一个应用。我正在尝试在我的网站中显示本地图像。我在 src 中创建了这个文件夹结构:assets/media/images/profile.jpg。图像不显示在浏览器中。这就是我正在做的事情。

这是src/components/Hero.js中的组件:

import ImageOne from '../assets/media/images/profile.jpg';

const Hero = () => {
return (
    <div className="bg-white h-screen flex flex-col justify-center items-center">
        <h1 className="lg:text-9xl md:text-7xl sm:text-5xl text-3xl font-black mb-14">
            Text
        </h1>

        <img class="rounded" src={ImageOne} alt='Profile'/>
        
    </div>
)
}

export default Hero

图像根本不显示。这是浏览器正在加载的内容:&lt;img class="rounded" src="/static/media/profile.ba8339ef.jpg" alt="Profile"&gt;

其他 stackoberflow 答案,如 React won't load local images,对我没有帮助。

【问题讨论】:

    标签: reactjs tailwind-css


    【解决方案1】:

    仔细检查顺风指南中的所有说明,确保将 react-scripts start 替换为 craco start

    https://tailwindcss.com/docs/guides/create-react-app

    同时,看看这个示例存储库:

    https://github.com/oieduardorabelo/2021-03-18-tailwindcss-and-create-react-app

    我创建了一个 assets/media/images/profile.jpgsrc/components/Hero.js 与您在此处共享的相同 sn-p,它工作正常?

    【讨论】:

    • 我一直在关注本教程,因为我是这些技术的新手:youtube.com/…,我还没有使用过craco start。在 package.json, scripts 中,我的开始是这样的:"start": "npm run watch:css &amp;&amp; react-scripts start",watch 是:"watch:css": "postcss src/assets/tailwind.css -o src/assets/main.css"
    • 实现了你的建议,没有运气。拉了github repo,我发现问题出在我正在导入的实际文件上:)
    【解决方案2】:

    将图片文件夹从 src/assets/media/ 移动到 public/ 并添加:

    <img class="rounded" src={process.env.PUBLIC_URL + '/images/profile.jpg'} alt='Profile'/>
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-04-30
    • 1970-01-01
    • 2022-11-23
    • 1970-01-01
    • 1970-01-01
    • 2020-05-10
    • 1970-01-01
    • 2016-01-17
    相关资源
    最近更新 更多