2016-08-01 73 views
0

我正在本地加載圖像,圖像文件位於jsx文件的同一文件夾中。該webpack.config.js是看起來是這樣的:Webpack img-loader無法加載圖像

path = require('path'); 

module.exports = { 
    context: __dirname, 
    entry: "./app/entry.jsx, 
    output: { 
    path: path.join(__dirname, 'lib'), 
    filename: "bundle.js", 
    devtoolModuleFilenameTemplate: '[resourcePath]', 
    devtoolFallbackModuleFilenameTemplate: '[resourcePath]?[hash]' 
    }, 
    module: { 
    loaders: [ 
     { 
     test: /\.jsx?$/, 
     exclude: /(node_modules|bower_components)/, 
     loader: 'babel', 
     query: { 
      presets: ['react'] 
     } 
     }, 
     { 
     test: /\.css$/, 
     loader: "style-loader!css-loader?root=." 
     }, 
     { 
     test: /\.(jpe?g|png|gif|svg)$/i, 
     loader: 'url?limit=10000!img?progressive=true' 
     } 
    ] 
    }, 
    devtool: 'source-map', 
    resolve: { 
    extensions: ["", ".js", ".jsx" ] 
    } 
}; 

和JSX是這樣

var loadingImg = require('url!img!./loading.gif'); 

錯誤顯示在控制檯:

ERROR in Loader app/node_modules/url/url.js didn't return a function 
@ app/components/session/login.jsx 5:17-49 

我應該如何加載此圖片?

+0

您不需要'require()'中定義的加載器。從文檔:「[在配置中指定加載程序是推薦的方法](https://webpack.github.io/docs/loaders.html)」 –

回答

0

沒有任何東西跳出來,因爲你的webpack配置文件不正確。我會看看你在代碼中調用圖像的方式,並不適合我。我不確定你試圖使用代碼的位置,但是你可以做類似的事情;

<img src={require("../../images/some-filepath/some-image.png")} /> 

或:

<Image source={{uri: '../../images/some-filepath/some-image.png'}} /> 

我也的確讓tutorial a while ago使用圖像的WebPack加載器,如果你想看看。

無論如何,希望這有助於!