javascript - 404 未找到(webpack 图片)

标签 javascript reactjs webpack build

如何使用此 Web 包配置导入图像?

使用 yarn build 构建应用程序后,我在控制台中收到以下错误。如何将图像从 assets/public 文件夹导入到仪表板文件? (请看图片)。提前致谢。

这是 webpack 配置文件。

const webpack = require('webpack');
const path = require('path');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');

const extractCSS = new ExtractTextPlugin('[name].fonts.css');
const extractSCSS = new ExtractTextPlugin('[name].styles.css');

const BUILD_DIR = path.resolve(__dirname, 'build');
const SRC_DIR = path.resolve(__dirname, 'src');

console.log('BUILD_DIR', BUILD_DIR);
console.log('SRC_DIR', SRC_DIR);

module.exports = (env = {}) => {
  return {
    entry: {
      index: [SRC_DIR + '/index.js']
    },
    output: {
      path: BUILD_DIR,
      filename: '[name].bundle.js',
    },
    // watch: true,
    devtool: env.prod ? 'source-map' : 'cheap-module-eval-source-map',
    devServer: {
      contentBase: BUILD_DIR,
      //   port: 9001,
      compress: true,
      hot: true,
      open: true
    },
    module: {
      rules: [
        {
          test: /\.(js|jsx)$/,
          exclude: /node_modules/,
          use: {
            loader: 'babel-loader',
            options: {
              cacheDirectory: true,
              presets: ['react', 'env']
            }
          }
        },
        {
          test: /\.html$/,
          loader: 'html-loader'
        },
        {
          test: /\.(scss)$/,
          use: ['css-hot-loader'].concat(extractSCSS.extract({
            fallback: 'style-loader',
            use: [
              {
                loader: 'css-loader',
                options: {alias: {'../img': '../public/img'}}
              },
              {
                loader: 'sass-loader'
              }
            ]
          }))
        },
        {
          test: /\.css$/,
          use: extractCSS.extract({
            fallback: 'style-loader',
            use: 'css-loader'
          })
        },
        {
          test: /\.(png|jpg|jpeg|gif|ico)$/,
          use: [
            {
              // loader: 'url-loader'
              loader: 'file-loader',
              options: {
                name: './img/[name].[hash].[ext]'
              }
            }
          ]
        },
        {
          test: /\.(woff(2)?|ttf|eot|svg)(\?v=\d+\.\d+\.\d+)?$/,
          loader: 'file-loader',
          options: {
            name: './fonts/[name].[hash].[ext]'
          }
        }]
    },
    plugins: [
      new webpack.HotModuleReplacementPlugin(),
      new webpack.optimize.UglifyJsPlugin({sourceMap: true}),
      new webpack.NamedModulesPlugin(),
      extractCSS,
      extractSCSS,
      new HtmlWebpackPlugin(
        {
          inject: true,
          template: './public/index.html'
        }
      ),
      new CopyWebpackPlugin([
          {from: './public/img', to: 'img'}
        ],
        {copyUnmodified: false}
      )
    ]
  }
};

请看下面的图片。

enter image description here

enter image description here

我在这里错过了什么?

提前致谢。

最佳答案

首先,您应该像这样导入图像:

import MyImage from '../relative/path/to/your/image'

然后像这样使用它:

function App() {
  return (
    <div className="App">
      <img src={MyImage} alt='any kind of description'>
    </div>
  );
}

关于javascript - 404 未找到(webpack 图片),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51916085/

相关文章:

javascript - Loopback/Strongloop 失败,ssh2 函数回调错误 : write ECONNABORTED

java - 从java内部访问javascript

javascript - 在 React 中使用 EventListener 和 postMessage 从弹出窗口传递数据

javascript - Vue.js - 是否可以使用 Javascript 动态导入从另一台服务器加载组件?

get - Webpack-dev-server 不允许 CORS 请求

javascript - 如何正确使用 ES6 "export default"和 CommonJS "require"?

javascript - C# WEB API CORS 不起作用

javascript - 合并排序的数组并删除重复的javascript

javascript - 预渲染 React 应用程序

javascript - 比较两个数组的 id | Reactjs