导入svg文件时,打字稿找不到模块

英斗

它是使用Typescript,React和webpack创建的Web应用程序。我想通过webpack使用svg文件。但是,我得到了

    TS2307: Cannot find module '~/images/slide.svg'. 

将打字稿文件放在/ frontend / src /上,然后成功构建打字稿文件。

然后,我在网站上尝试了一些解决方案。但是我不能解决这个问题。我尝试的内容简要描述如下。

我在“ typeRoots”中添加了定义类型的文件。tsconfig.json

{
  "compilerOptions": {
    "outDir": "./app/assets/javascripts/bundles",
    "sourceMap": true,
    "allowJs": true,
    "allowSyntheticDefaultImports": true,
    "noImplicitAny": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "moduleResolution": "node",
    "module": "es2015",
    "target": "es6",
    "jsx": "react",
    "removeComments": true,
    "baseUrl": ".",
    "paths": {
      "~/*": ["./frontend/src/*"]
    },
    "typeRoots": ["types", "node_modules/@types"]
  },
  "awesomeTypescriptLoaderOptions": {
    "useCache": true,
    "cacheDirectory": "tmp/build_caches/awesome-typescript-loader"
  },
  "include": [
    "./frontend/src/*"
  ],
  "exclude": [
    "node_modules",
    "bundles",
    "**/*.js"
  ]
}

类型/images.d.ts

declare module '*.svg' {
  const content: any;
  export default content;
}

我在webpack上使用“文件加载器”来构建svg fild。

请帮我!!

迈尔

我认为这是因为您在Typescript配置中的include部分。尝试这个:

"include": [
  "./frontend/src/*/**"
],

否则,您只包括键入src文件夹顶层中的文件。而且您的svg输入文件位于子级别中,因此Typescript不会包含该文件。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章