简体   繁体   English

如何使用Webpack-2和React.js显示图像

[英]How to display an image using webpack-2 with React.js

How do I load an image (jpg) file within a React.js project? 如何在React.js项目中加载图像(jpg)文件? I saved the image in /src/images with a filename of, iphone-template.jpg . 我将图像保存在/src/images ,文件名为iphone-template.jpg

webpack.config.js webpack.config.js

const { resolve } = require('path');
const webpack = require('webpack');

module.exports = {
  entry: [
    'react-hot-loader/patch',
    // activate HMR for React

    'webpack-dev-server/client?http://localhost:8080',
    // bundle the client for webpack-dev-server
    // and connect to the provided endpoint

    'webpack/hot/only-dev-server',
    // bundle the client for hot reloading
    // only- means to only hot reload for successful updates

    './index.js'
    // the entry point of our app
  ],
  output: {
    filename: 'bundle.js',
    // the output bundle

    path: resolve(__dirname, 'docs'), // changed 'dist' to 'www'

    publicPath: '/'
    // necessary for HMR to know where to load the hot update chunks
  },

  context: resolve(__dirname, 'src'),

  devtool: 'inline-source-map',
  // devtool: "source-map"

  devServer: {
    hot: true,
    // enable HMR on the server

    contentBase: resolve(__dirname, 'docs'), // changed 'dist' to 'www'
    // match the output path

    publicPath: '/'
    // match the output `publicPath`
  },

  module: {
    rules: [
      {
        test: /\.js$/,
        use: [ 'babel-loader'],
        exclude: /node_modules/
      },
      {
        test: /\.css$/,
        use: [ 'style-loader', 'css-loader'],
      },
      {
        test: /\.(png|woff|woff2|eot|ttf|svg)$/,
        loader: 'url-loader?limit=100000'
      },

      // the below webpack config was sourced from this,
      // https://github.com/coryhouse/react-slingshot/issues/128
      // in order to load favicon.
      {
          test: /\.jpe?g$|\.ico$|\.gif$|\.png$|\.svg$|\.woff$|\.ttf$|\.wav$|\.mp3$/,
          loader: 'file-loader?name=[name].[ext]'  // <-- retain original file name
      },
      {
        test: /\.(png|jpg|)$/,
        loader: 'file-loader?name=/images/[name].[ext]'
      },
      {
        test: /\.(png|jpg|gif)$/,
        loader: "file-loader?name=img/img-[hash:6].[ext]"
      },
      {
        test: /\.(jpg|png|svg)$/,
        loader: 'file',
        include: './src/images'
}
    ],
  },

  plugins: [
    new webpack.HotModuleReplacementPlugin(),
    // enable HMR globally

    new webpack.NamedModulesPlugin(),
    // prints more readable module names in the browser console on HMR updates

    new webpack.ProvidePlugin({
      $: 'jquery',
      jQuery: 'jquery'
    }),
  ],
};

App.js App.js

import iPhone from '../images/iphone-template.jpg';

    const App = (props) => {
      return (
        <div id="parent">
          <NavBar {...navbar} />
          {/* see scratchpad.txt for removed div. */}
          <div id="iphone">
            {/*<img src={iPhone} />*/}
            <img className="iphone-template" src={iPhone} />
          </div>

I'm seeing an error image like the one below. 我看到类似下面的错误图像。 错误

If it's indeed webpack 2, you'll need to use file-loader in all the names of your loaders, because it no longer supports just file or url to use the loaders. 如果确实是webpack 2,则需要在所有file-loader名称中使用file-loader ,因为它不再仅支持fileurl来使用加载程序。

Other than that, you have several of them affecting your .jpg .png .gifs, etc .. you can get rid of the duplication and just write one for them all, as all you are doing effectively is copying the files (no optimization, no hash renaming), unless you'd want copies on your root, your images and your img directories within distribution 除此之外,您还有其他几种会影响您的.jpg .png .gifs等。您可以摆脱重复,只为它们全部写一个,因为您要做的就是复制文件(没有优化,没有哈希重命名),除非您希望在发行版的根目录,图像和img目录中复制副本

But here is the question, how are you accessing the page to display? 但是这里的问题是,您如何访问要显示的页面?

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

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