React&Webpack:加载并显示图像作为背景图像

问题描述:

我创建了一个带有图像设置作为组件背景的横幅状组件,但是我无法使其运行。我尝试了在线发布的不同建议,但没有运气,目前我不确定我的错误是否在反应代码中,或者是没有正确加载文件的webpack。React&Webpack:加载并显示图像作为背景图像

这是我的文件结构:

AppFolder 
- client/ 
-- components/ 
-- data/ 
-- images/ 
----- banners/ 
------- frontPageBanner2.png 
    ------- 
-- styles/ 
-- myApp.js 
-- store.js 
    --------- 
- index.html 
- package.json 
- webpack.config.dev.js 

这里是我的WebPack配置:

var path = require('path'); 
var webpack = require('webpack'); 

module.exports = { 
    devtool: 'source-map', 
    entry: [ 
    'webpack-hot-middleware/client', 
    './client/myApp' 
    ], 
    output: { 
path: path.join(__dirname, 'dist'), 
filename: 'bundle.js', 
publicPath: '/static/' 
    }, 
    plugins: [ 
    new webpack.HotModuleReplacementPlugin(), 
    new webpack.NoErrorsPlugin() 
    ], 
    module: { 
    loaders: [ 
    // js 
    { 
     test: /\.js$/, 
     exclude: /node_modules/, 
     loaders: ['babel'], 
     presets: ['es2015', 'react'], 
     include: path.join(__dirname, 'client') 
    }, 
    // CSS 
    { 
     test: /\.scss$/, 
     include: path.join(__dirname, 'client'), 
     loader: 'style-loader!css-loader!sass-loader' 
    }, 

    // PNG 
    { 
     test: /\.(jpg|png)$/, 
     loader: "url-loader?limit=25000", 
     //loader: 'file?name=[path][name].[ext]', 
     include: path.join(__dirname, 'client') 
    }, 

    // FontAwesome 
    { test: /\.woff(2)?(\?v=[0-9]\.[0-9]\.[0-9])?$/, loader: "url-loader?limit=10000&mimetype=application/font-woff" }, 
    { test: /\.(ttf|eot|svg)(\?v=[0-9]\.[0-9]\.[0-9])?$/, 
     exclude: path.join(__dirname, 'client/images'), 
     loader: "file-loader" }, 

    // other SVG 
    { test: /\.svg$/, 
     loader: 'url-loader', 
     query: { mimetype: "image/svg+xml" }, 
     include: path.join(__dirname, 'client/images') 
    }, 

    ] 
    } 
}; 

这里就是我如何使用它的应用程序:

export class Banner extends React.Component { 
    render() { 
     console.log(this.props.banners); 
     // = '../images/banners/frontPageBanner.png' 
     const bannerImg = this.props.image.backgroundImage; 

     var bannerStyle = { 
      background: 'url(' + bannerImg + ')', 
      backgroundSize: this.props.image.backgroundSize, 
      backgroundPosition: this.props.image.backgroundPosition, 
     } 

     return (
      <section key={this.props.key} className="container hero" style={bannerStyle}> 
       <div className="textbox"> 
        <h2>{this.props.title}</h2> 
       </div> 
      </section> 
     ) 
    } 
} 

这里的生成的HTML :

<section class="container hero" style="background: url('../images/banners/frontPageBanner2.png') 100% 100%/contain;"> 
    ... 
</section> 

但没有显示图像。同时打开图像的src链接只显示一个空白屏幕。为什么?我该如何解决它?

您应该传递一个var,这是需要图像的结果,当您需要使用webpack时,它会生成一个base64内联图像,而不是相对路径。

var DuckImage = require('./Duck.jpg'); 

var bannerStyle = { 
    backgroundImage: 'url(' + DuckImage + ')', 
    backgroundSize: this.props.image.backgroundSize, 
    backgroundPosition: this.props.image.backgroundPosition, 
} 
+0

我试过了,是的,如果我手动插入一个路径到'require()'它工作。但是,该组件应该是动态的,并且必须从道具中读取URL。 'require(this.props.image.backgroundImage)'不起作用... – spik3s

+1

您可以随时加载图片并传递给道具,但如果网址真的是动态的,并且不应该捆绑到您的应用中,那么您绝对需要传递绝对路径 - 例如“http://localhost/myapp/public/img/myimage.jpg”。那么你将不需要使用require,你的应用会变得更小。 –

+0

这就是我最终做的事情,我使用绝对路径来提供来自同一个服务器的照片,以及带有URL的JSON数据。感谢您的帮助! – spik3s