首页 > 解决方案 > React CSS 不能使用公共图片

问题描述

我创建了一个项目create-react-app project_name,然后我尝试使用公共文件夹中的图像作为背景,并在 css 中使用此代码 background-image: url('/images/ernestDrum.jpeg');

然后我 ./src/App.css (./node_modules/css-loader/dist/cjs.js??ref--5-oneOf-4-1!./node_modules/postcss-loader/src??postcss!./src/App.css) Error: Can't resolve '/images/ernestDrum.jpeg' in 'E:\project\ernest\src' 在以前的项目中遇到了这个错误,我使用相同的代码并且它有效

标签: reactjs

解决方案


将 react-scipts 从 3.xx 更新到 4.xx 后我遇到了同样的错误
并发现了这个问题https://github.com/facebook/create-react-app/issues/9870

回滚到 3.xx 可以解决这个问题


推荐阅读