[英]Unable to display photo in react create app project
I can't view the photos in my project that I created with create react app.我无法查看我使用 create react app 创建的项目中的照片。 please help me..请帮我..
<img id="2" src={require('../../../img/logo.png')} className="img-fluid" alt="logo"/>
the file path is absolutely correct.文件路径是绝对正确的。
package.json:包.json:
{
"name": "e-ticaret-react-ui",
"version": "0.1.0",
"private": true,
"dependencies": {
"@testing-library/jest-dom": "^5.15.0",
"@testing-library/react": "^11.2.7",
"@testing-library/user-event": "^12.8.3",
"axios": "^0.24.0",
"bootstrap": "^5.1.3",
"jquery": "^3.6.0",
"react": "^17.0.2",
"react-bootstrap-icons": "^1.6.1",
"react-dom": "^17.0.2",
"react-redux": "^7.2.6",
"react-router-dom": "^5.3.0",
"react-scripts": "4.0.3",
"redux": "^4.1.2",
"web-vitals": "^1.1.2"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
A photo was displayed in the React project created by Visual Studio.在 Visual Studio 创建的 React 项目中显示了一张照片。 Photos are not displayed in the project I created with create react app in vs code.我在 vs 代码中使用 create react app 创建的项目中没有显示照片。 The file structure is exactly the same.文件结构完全一样。 the folder paths are correct.文件夹路径是正确的。
Try this:尝试这个:
At the top of your file with your import
s use this line:在带有import
的文件顶部使用以下行:
import Logo from '../../../img/logo.png';
And then your img
would look like this:然后你的img
看起来像这样:
<img id="2" src={ Logo } className="img-fluid" alt="logo"/>
Create a folder calls assets
then put on their images inside, like this:创建一个文件夹调用assets
然后把它们的图片放在里面,像这样:
import Logo from '../../assets/logos/logo.png';
<img id="2" src={ Logo } className="img-fluid" alt="logo"/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.