简体   繁体   English

批量反应导入图标/图像

[英]React Import Icons / Images in bulk

I have 100's of Icons and Images to be imported. 我有100个要导入的图标和图像。 Is there any way to eliminate writing so many import statements at the top of the page? 有没有办法消除在页面顶部写这么多import语句? I was thinking to write a import statements in a separate file and embed that at the top. 我想在一个单独的文件中编写一个import语句并将其嵌入顶部。

import basicAmenitiesIcon from '../../../../images/icons/wifi-sign.png';
import parkingIcon from '../../../../images/icons/parking.png';
...

Any other way of solving it? 还有其他解决方法吗? I'm using webpack and here is the config: 我正在使用webpack,这是配置:

{
    test: /\.(jpe?g|png|gif|svg)$/i,
    loaders: [
        'file?hash=sha512&digest=hex&name=[hash].[ext]',
        'image-webpack?bypassOnDebug&optimizationLevel=7&interlaced=false'
    ]
}

Yes, it is posible, see my answer here: https://stackoverflow.com/a/41410938/646156 是的,它是可行的,请参阅我的答案: https//stackoverflow.com/a/41410938/646156

var context = require.context('../../../../images/icons', true, /\.(png)$/);
var files={};

context.keys().forEach((filename)=>{
  files[filename] = context(filename);
});
console.log(files); //you have file contents in the 'files' object, with filenames as keys

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

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