首页 > 解决方案 > 我无法在 React / Typescript 上将图像导入我的模块,只需要作品

问题描述

这在 UI 级别上没有问题:

import profileImg from '../../assets/murdoch.jpg';

但是我在 VSCode 上收到此错误:

找不到模块'../../assets/murdoch.jpg'.ts(2307)

这在 UI 和 VSCode 上都没有问题:

const profileImg = require('../../assets/murdoch.jpg');

但我想导入它,因为测试没有通过

    SyntaxError: Invalid or unexpected token

      3 | import ProfilePicture from './ProfilePicture';
      4 |
    > 5 | const profileImg = require('../../assets/murdoch.jpg');

这是我的tsconfig.json

{
  "compilerOptions": {
    "sourceMap": true,
    "noImplicitAny": false,
    "module": "commonjs",
    "removeComments": true,
    "baseUrl": "./",
    "allowJs": true,
    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true,
    "isolatedModules": false,
    "jsx": "react",
    "lib": ["es6", "dom"],
    "moduleResolution": "node",
    "noEmit": true,
    "strict": true,
    "target": "esnext"
  },
  "exclude": ["node_modules", "jest.config.js", "webpack.config.js"]
}

我错过了什么?

编辑:

项目树

在此处输入图像描述

标签: javascriptreactjstypescriptecmascript-6

解决方案


推荐阅读