【问题标题】:Electron loads index.html but not the rest of the bundle filesElectron 加载 index.html 但不加载其余的捆绑文件
【发布时间】:2019-09-01 06:57:07
【问题描述】:

我们有一个电子应用程序并希望使用loadUrl() 加载渲染器包。

win=new BrowserWindow({ /* ... */ })
win.loadURL(`file://${path.join(__dirname, '../../render/build/index.html')}`);

在 html 文件中,我们加载 React Bundle

<script type="text/javascript" src="/js/app-my-hash.bundle.js"></script>

但是,正如预期的那样,找不到该文件,因为我想我需要以某种方式设置项目的根目录。我收到此错误

Failed to load resource: net::ERR_FILE_NOT_FOUND

我在电子端(或 webpack 端)缺少什么设置才能使其正常工作?

【问题讨论】:

  • 找不到哪个文件?如果 index.html,如果您使用绝对地址而不是 path.join,它会加载吗?还是 app-my-hash.bundle.js ?如果是,它在根 js 文件夹中吗?您是否尝试使用 require('myfile.js') 加载它?
  • JS 丢失。它不在根 js 中。它位于相对于 index.html 的 js 文件夹中。我没有尝试用require加载它,它是由webpack捆绑的。
  • 尝试相对 URL,例如:src="js/app-my-hash.bundle.js"src="./js/app-my-hash.bundle.js"
  • 是的。那行得通。
  • 太棒了。我把它作为答案。

标签: javascript reactjs electron


【解决方案1】:

使用相对 URL,例如:src="js/app-my-hash.bundle.js"src="./js/app-my-hash.bundle.js"

【讨论】:

    【解决方案2】:

    index.html 中添加相对路径而无需手动更新的另一种方法是包含,

    package.json 中的

    homepage 属性。

    举个例子,

    {
      "name": "electron-project",
      "homepage": "./", # Add this line
      #.... all remaining properties
    }
    

    Stackoverflow Source

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-29
      • 2014-11-06
      • 2013-06-16
      • 2017-10-22
      • 2017-12-14
      相关资源
      最近更新 更多