简体   繁体   English

在反应组件中找不到模块

[英]Cannot find module in react component

When I try to put my image to react component I see Cannot find module '../../assets/images/logo.png' or its corresponding type declarations.当我尝试将图像放入反应组件时,我看到Cannot find module '../../assets/images/logo.png' or its corresponding type declarations. . . I've added declare module *.png in.d.ts file but it still not working.我添加了declare module *.png in.d.ts 文件,但它仍然无法正常工作。 I start with webapack so my problem is possibly very stupid but I really need help.我从 webapack 开始,所以我的问题可能非常愚蠢,但我真的需要帮助。 When I remove "include": ["src/**/*"] from tsconfig, everything is ok.当我从 tsconfig 中删除"include": ["src/**/*"]时,一切正常。 What can be wrong?有什么问题?

Webpack: Webpack:

const path = require('path')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const webpack = require('webpack')
const ErrorOverlayPlugin = require('error-overlay-webpack-plugin')


module.exports = {
    entry: '/src/index.tsx',
    output: {
        path: path.resolve(__dirname, 'dist'),
        filename: 'bundle.js'
    },
    resolve: {
        extensions: [".ts", ".tsx", ".js"]
    },
    devtool: 'cheap-module-source-map',
    module: {
        rules: [
            {
                test: /\.tsx?/,
                loader: 'ts-loader'
            },
            {
                test: /\.js/,
                exclude: (/node_modules/),
                loader: 'babel-loader'
            },
            {
                test: /\.s[ac]ss$/i,
                use: ["style-loader", "css-loader", "sass-loader",],
            },
            {
                test: /\.(png$|jpe?g|gif)$/i,
                use: [
                    {
                        loader: 'file-loader',
                        options: {
                            name: '[name].[ext]',
                            outputPath: 'assets/',
                            publicPath: 'assets/'
                        }
                    }
                ]
            },
        ],
    },
    plugins: [
        new HtmlWebpackPlugin({
            filename: 'index.html',
            template: 'src/index.html'
        }),
        new webpack.HotModuleReplacementPlugin(),
        new ErrorOverlayPlugin()
    ],
    devServer: {
        contentBase: path.join(__dirname, 'dist'),
        compress: true,
        port: 8080,
        open: true,
        historyApiFallback: true,
        hot: true,
        overlay: true,
    }
} 

tsconfig.json: tsconfig.json:

{
    "compilerOptions": {
        "module": "commonjs",
        "target": "es5",
        "noImplicitAny": true,
        "removeComments": true,
        "preserveConstEnums": true,
        "sourceMap": false,
        "jsx": "react",
        "esModuleInterop": true,
        "baseUrl": "src",
        "typeRoots" : ["node_modules/@types", "src/types"],
        "allowSyntheticDefaultImports": true,
    },
    "include": ["src/**/*"],
    "exclude": ["build"],
}

React:反应:

import React, { FC } from 'react'
import { NavLink } from 'react-router-dom'
import logo from '../../assets/images/logo.png'                    
     
const Header: FC = () => { 
    return (   
        <div className="header" id='header'>   
            <div className="logo">     
                <img src={logo} alt="logo"/>  
                <h1>Logo</h1>
            </div>
        </div>
    );
}
 
export default Header;

All the time I see photo on page but error still exist.我一直在页面上看到照片,但错误仍然存在。

Create a declarations.d.ts file inside src/ and add declare module '*.png';src/中创建一个declarations.d.ts文件并添加declare module '*.png'; to it.给它。

That should solve the problem as user defined declarations should go inside the src/ by default.这应该可以解决问题,因为默认情况下,用户定义的声明应该 go 在src/内。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM