这是一个使用Typescript、React和webpack创建的Web应用程序。我想通过webpack使用svg文件。但是,我遇到了
TS2307: Cannot find module '~/images/slide.svg'.
Typescript文件位于/frontend/src/,并且Typescript文件已经成功构建。
然后,我在网站上尝试了一些解决方案。但是我无法解决这个问题。 我尝试的方法简要描述如下。
我添加了一个文件,在其中定义了'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"
]
}
类型/图片.d.ts
declare module '*.svg' {
const content: any;
export default content;
}
我在webpack中使用 'file-loader' 来构建SVG文件。请帮帮我!!