我的 webpack.config.js 无法读取任何图像文件 - Angular 8

My webpack.config.js cant read any image file - Angular 8

我正在构建一个 Angular 8 应用程序,但是在构建它时出现错误。我不知道如何在 webpack 中加载图像。我已经尝试了一些方法,但没有任何效果。我收到此错误:

ERROR in ./src/assets/images/icon_transparent.png 1:0
Module parse failed: Unexpected character '�' (1:0)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
(Source code omitted for this binary file)
 @ ./src/app/shared/navigation/navigation.component.html 1:265-326
 @ ./src/app/shared/navigation/navigation.component.ts
 @ ./src/app/app.module.ts
 @ ./src/main.ts

我使用的 webpack 版本会影响这个吗?

我的webpack.config.js是这个:

const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const path = require('path');

module.exports = {
    entry: './src/main.ts',
    resolve: {
        extensions: ['.ts', '.js'],
        alias: {
            '@': path.resolve(__dirname, 'src/app/'),
        }
    },
    module: {
        rules: [
            {
                test: /\.ts$/,
                use: ['ts-loader', 'angular2-template-loader']
            },
            {
                test: /\.html$/,
                use: 'html-loader'
            },
            {
                test: /\.less$/,
                use: ['style-loader', 'css-loader', 'less-loader']
            },

            // workaround for warning: System.import() is deprecated and will be removed soon. Use import() instead.
            {
                test: /[\/\]@angular[\/\].+\.js$/,
                parser: { system: true }
            }
        ]
    },
    plugins: [
        new HtmlWebpackPlugin({ template: './src/index.html' }),
        new webpack.DefinePlugin({
            // global app config object
            config: JSON.stringify({
                apiUrl: 'http://localhost:4000'
            })
        }),

        // workaround for warning: Critical dependency: the request of a dependency is an expression
        new webpack.ContextReplacementPlugin(
            /\@angular(\|\/)core(\|\/)fesm5/,
            path.resolve(__dirname, 'src')
        )
    ],
    optimization: {
        splitChunks: {
            chunks: 'all',
        },
        runtimeChunk: true
    },
    devServer: {
        historyApiFallback: true
    }
}

应该用作外部资源(如图像、字体等)而不是捆绑到 javascript 代码中的文件必须使用适当的 webpack loader. Usually it is the webpack's native file-loader 加载。

// add this to your rules array
{
  test: /\.(png|jpe?g|gif|svg|woff|woff2|ttf|eot|ico)$/,
  use: 'file-loader'
}

More about loading images with webpack.

也可以考虑使用 angular-cli 而不是 webpack。它通常工作得更快,你不会浪费时间编写 webpack 配置。