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

IT技术 reactjs image electron
2021-05-15 12:19:02

我想从 Application Support 文件夹中动态地要求图像。我使用 react-app-rewired 包在 src 文件夹之外要求,但是当我在图像 src 中使用窗口变量时,我收到错误:错误:找不到module。我得到这样的路径并得到一个带有字符串的错误代码,当我直接在图像标签的 src 部分使用它时,该字符串有效。

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

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

图像(作品):

<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/Application Support/template-editor”,这也可以正常工作

1个回答
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此变量。但此变量仅在您使用电子构建器打包应用程序时才可用。