它是使用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"
]
}
types / images.d.ts
declare module '*.svg' {
const content: any;
export default content;
}
我在webpack上使用“文件加载器”来构建svg fild。
请帮助我!