简体   繁体   English

如何在 src 文件夹之外需要带有反应电子的图像(应用程序支持)

[英]How to require an image with react electron outside of src folder(Application Support)

I want to require images dynamically from the Application Support folder.我想从 Application Support 文件夹中动态地要求图像。 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.我使用 react-app-rewired 包要求在 src 文件夹之外,但是当我在图像 src 中使用窗口变量时,我收到错误:错误:找不到模块。 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.我得到这样的路径并得到一个带有字符串的错误代码,当我直接在图像标签的 src 部分使用它时,该字符串有效。

window.path = window.electron.remote.app.getPath("appData")

image(Does not work):图像(不起作用):

<img className="topSvg" src={require(""+window.path+'/current/'+this.props.project+'/src/content/img/changeable/'+data.top.img)} alt="top_svg"/>

Error: Cannot find module '/Users/tobi/Library/Application Support/template-editor/current/Tobi/src/content/img/changeable/top_svg.svg ' Error: Cannot find module '/Users/tobi/Library/Application Support/template-editor/current/Tobi/src/content/img/changeable/top_svg.svg

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"/>

I also tried to just replace the window.path with the string '/Users/tobi/Library/Application Support/template-editor' which also works fine我还尝试将 window.path 替换为字符串 '/Users/tobi/Library/Application Support/template-editor' 也可以正常工作

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', ...);
    }
}

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.如果您要获取应用程序的位置,请使用此process.env.PORTABLE_EXECUTABLE_DIR但此变量仅在您使用电子构建器打包应用程序时可用。

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

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