【发布时间】:2020-01-07 22:32:15
【问题描述】:
我正在尝试获取图像路径,但无法获取。我的代码文件在 src 的组件文件夹中,我的图像文件夹也在 src 文件夹中
function ImageWithText() {
render(){
return(
<div class="col-md-6">
<h2>Lorem</h2>
<p>Lorem Ispum</p>
</div>
<div class="col-md-6">
<img scr={require('./../images/foodone.jpg')} alt=""/>
</div>
);
}
}
export default ImageWithText;
目录结构是
src-
components-
TextWithImage.js
images-
foodone.jpg
错误是
./src/Components/ImageWithText.js 未找到模块:无法解析“D:\react work\react javascript\react-tu\src\Components”中的“./images/foodone.jpeg”
【问题讨论】:
-
把你的组件代码贴在你用过图片的地方。
-
您正在使用
./images,它将在组件文件夹中查找图像。你应该使用 ../images/foodone.jpeg -
试试这个 -
<img scr={require('../images/foodone.jpeg')} alt=""/> -
行得通!现在没有错误但图像没有出现在前端
-
@mohitchandel,检查图片名称和扩展名是否正确。
标签: reactjs