React img 显示 [object Module] 而不是图像

C.R*_*ldo 3 reactjs

最近,我将我的项目推送到了 Gitlab,这样我也可以在我的笔记本电脑上处理它来练习我的 React。在我拉出并 npm 安装了所有东西之后(不得不使用npm audit fix --force这很奇怪),我尝试运行它。一切看起来都很好,但图像不会显示,当我检查它时,它显示[对象模块]。现在我已经用谷歌搜索了它,大多数答案都提到了将选项更改为

options: {
      esModule: false,
    },
Run Code Online (Sandbox Code Playgroud)

在网络包中

问题是,我认为我从未使用过 webpack,并且在我的桌面上它工作正常(我没有 webpack.config.js)。这是我的 package.json,与我的笔记本电脑上的相同:

{
  "name": "test",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@testing-library/jest-dom": "^4.2.4",
    "@testing-library/react": "^9.3.2",
    "@testing-library/user-event": "^7.1.2",
    "axios": "^0.19.2",
    "bootstrap": "^4.5.0",
    "cors": "^2.8.5",
    "node-sass": "^4.14.1",
    "react": "^16.13.1",
    "react-dom": "^16.13.1",
    "react-scripts": "3.4.1",
    "react-select": "^3.1.0"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  },
  "devDependencies": {}
}
Run Code Online (Sandbox Code Playgroud)

Dav*_*par 6

我使用“默认”属性来访问字符串值(路径):

<img 
   src={require("../../assets/images/svg/shopping-cart.svg").default}
   alt="Shopping cart"/>
Run Code Online (Sandbox Code Playgroud)

这个解决方案是我从Carlos Avila对 vuejs GitHub 问题的评论中获取的。