如何在src文件夹之外要求具有反应电子的图像(应用程序支持) [英] How to require an image with react electron outside of src folder(Application Support)
问题描述
我想从应用程序支持文件夹中动态请求图像。我使用react-app-rewired程序包在src文件夹之外要求,但是当我在图像src中使用窗口变量时,出现错误:错误:找不到模块。我得到了这样的路径,并获得了一个错误代码,该错误代码带有一个字符串,当我直接在image标记的src部分中使用它时,该字符串可以工作。
I want to require images dynamically from the Application Support folder. I use the react-app-rewired package to require outside of the src folder but when I use a window variable in the image src I get the error: Error: Cannot find module. I get the path like this and get an error code with a string that works when I use it directly in the src part of the image tag.
window.path = window.electron.remote.app.getPath("appData")
图片(无效):
<img className="topSvg" src={require(""+window.path+'/current/'+this.props.project+'/src/content/img/changeable/'+data.top.img)} alt="top_svg"/>
错误:找不到模块'/ Users / tobi / Library / Application Support /template-editor/current/Tobi/src/content/img/changeable/top_svg.svg
'
image(works):
image(works):
<img className="topSvg" src={require('/Users/tobi/Library/Application Support/template-editor/current/Tobi/src/content/img/changeable/top_svg.svg')} alt="top_svg"/>
我也尝试将window.path替换为字符串'/ Users / tobi / Library /应用程序支持/模板编辑器也可以正常工作
I also tried to just replace the window.path with the string '/Users/tobi/Library/Application Support/template-editor' which also works fine
推荐答案
switch(process.platform) {
case 'darwin': {
return path.join(process.env.HOME, 'Library', 'Application Support', 'template-editor', ...);
}
case 'win32': {
return path.join(process.env.APPDATA, 'template-editor', ...);
}
case 'linux': {
return path.join(process.env.HOME, '.template-editor', ...);
}
}
如果要获取您的位置,应用程序,然后使用此
process.env.PORTABLE_EXECUTABLE_DIR
,但是仅当您使用电子生成器打包应用程序时,此变量才可用。
And if you are going to get the location of your app then use this
process.env.PORTABLE_EXECUTABLE_DIR
But this variable will be only available when you used electron-builder to pack your app.
这篇关于如何在src文件夹之外要求具有反应电子的图像(应用程序支持)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!