gpt4 book ai didi

javascript - React 批量导入图标/图像

转载 作者:行者123 更新时间:2023-11-29 10:33:15 24 4
gpt4 key购买 nike

我有 100 个图标和图像要导入。有什么办法可以避免在页面顶部写这么多导入语句吗?我想在一个单独的文件中编写导入语句并将其嵌入顶部。

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

还有其他解决方法吗?我正在使用 webpack,这里是配置:

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

最佳答案

是的,这是可能的,请在此处查看我的回答: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

关于javascript - React 批量导入图标/图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41409275/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com