简体   繁体   中英

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. please help me..

<img id="2" src={require('../../../img/logo.png')} className="img-fluid" alt="logo"/>

the file path is absolutely correct.

package.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. Photos are not displayed in the project I created with create react app in vs code. 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 Logo from '../../../img/logo.png';

And then your img would look like this:

<img id="2" src={ Logo } className="img-fluid" alt="logo"/>

Create a folder calls assets then put on their images inside, like this:

import Logo from '../../assets/logos/logo.png';

<img id="2" src={ Logo } className="img-fluid" alt="logo"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM