![](/img/trans.png)
[英]Cannot find module '../../images/Logo.png'.ts(2307) Red Error is Still showing
[英]TS2307: Cannot find module './images/logo.png'
我正在尝试将本地 png 图像导入到我的 ts webpack 项目中,但出现以下错误。
TS2307: Cannot find module './images/logo.png'.
我所有的其他模块都可以正常导入,即; 我的 css、svg 和 ts 文件。 它似乎只发生在 png 上。
我的 webpack.config.js 模块部分
module: {
rules: [{
test: /\.ts$/,
use:['ts-loader']
},{
test: /\.css$/,
use: ['style-loader', 'css-loader']
},{
test: /\.(woff|woff2|eot|ttf|svg)$/,
use: ['url-loader?limit=100000']
},{
test: /\.png$/,
use: ['file-loader']
}]
}
我的 tsconfig.json
{
"compilerOptions": {
"outDir": "./dist/",
"sourceMap": true,
"module": "CommonJS",
"target": "ES5",
"lib": ["DOM", "ES2015.Promise", "ES5"],
"allowJs": true,
"alwaysStrict": true
}
}
我的进口声明
import Logo from './images/logo.png';
我的文件结构
root
-src
--css
--images
---logo.png
--index.ts
--templates.ts
-package.json
-tsconfig.json
-webpack.config.js
我在这里找到了答案。 Webpack 和 Typescript 图像导入
这就是我所做的。
添加了一个新目录和一个 import-png.d.ts 文件
root
-typings
--custom
---import-png.d.ts
导入-png.d.ts
declare module "*.png" {
const value: any;
export default value;
}
将我的文件加载器模块更改为:
{
test: /\.(png|jpe?g|gif|jp2|webp)$/,
loader: 'file-loader',
options: {
name: 'images/[name].[ext]'
}
现在我可以用这样的语句导入:
import Logo from './images/logo.png'
我的错误是我排除了参考 react-app-env.d.ts:
/* eslint-disable spaced-comment */
/// <reference types="react-scripts" />
由于间隔注释规则,Eslint 促使我这样做。
希望这可以帮助某人。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.