Mar*_*nec 4 javascript frontend image node.js reactjs
我<img src={require('./somRelativePath/image.jpg)}在 React 中使用过很多次。不过,这次好像不行了。没有任何错误(例如找不到图像等),但网站上的图像损坏。
检查元素后,我对浏览器中的转换结果有些困惑:
<img src="[object Module]" style="width: 5rem;">
Run Code Online (Sandbox Code Playgroud)
看起来好像它将图像作为组件而不是实际文件加载。我已经创建了该应用程序,npx create-react-app但到目前为止还没有弹出它。所以在 babel 或 webpack 配置中没有错误,因为它目前是由 react 在后台处理的。
用import语句导入它工作得很好:
import calendarPic from '../assets/pictures/calendar.svg';
Run Code Online (Sandbox Code Playgroud)
不幸的是,这不是解决方案,因为我将本地图像保存在 json 中,并且加载所有 50 个图像肯定会非常重复且无效。
同样,npx create-react-app我以前做过一些小项目,但从未遇到过如此令人困惑但如此基本的错误。我将非常感谢您的任何回应,因为我已经浏览了整个互联网上的所有可能的解决方案。
再次感谢您,祝您有美好的一天!
小智 12
使用这个,它对我有用
<img src={require('./somRelativePath/image.jpg').default}
解释 :
值 fromlet image1require('./somRelativePath/image.jpg')是不同的
import calendarPic from './somRelativePath/image.jpg';
如果您安慰他们,则 value fromcalendarPic是一个路径,但如果您使用require,则 value 是一个对象,例如 here。
| 归档时间: |
|
| 查看次数: |
6699 次 |
| 最近记录: |