electron 电子邮件:无法加载URL:http://localhost:3000/main_window出现错误:生产模式下的ERR_CONNECTION_REFUSED

5kgi1eie  于 2023-01-06  发布在  Electron
关注(0)|答案(2)|浏览(1350)

使用可执行文件(yarn make),我得到这个错误:

electron: Failed to load URL: http://localhost:3000/main_window with error: 
ERR_CONNECTION_REFUSED

此错误不会在开发模式中显示
我尝试在main中区分开发模式和生产模式:

import isDev from 'electron-is-dev' 
 const createWindow = (): void => {
  // Create the browser window.
  const mainWindow = new BrowserWindow({
    height: 600,
    width: 800,
    webPreferences: {
      preload: MAIN_WINDOW_PRELOAD_WEBPACK_ENTRY
    }
  });

  // and load the index.html of the app.
  isDev ? mainWindow.loadURL(MAIN_WINDOW_WEBPACK_ENTRY) : `file://${path.join(__dirname, '../build/index.html')}`

  // Open the DevTools.
  if (isDev ) {
    mainWindow.webContents.openDevTools();
  }
};

但在这种情况下,错误消息不会显示,而且正确窗口的页面Electron App也不会显示:
处于开发模式:窗口页面中显示"Got Finally!!"消息,

但不在生产模式下

这意味着该页未正确加载
其他信息:

Electron: 19
O.S. : Ubuntu 22.04

如何让它发挥作用?
更新1)
感谢@Bets的评论,我意识到我犯了两个错误(文件夹和三元运算符),我相应地更新了代码行:

mainWindow.loadURL(isDev ? MAIN_WINDOW_WEBPACK_ENTRY : `file://${path.join(__dirname, '../renderer/main_window/index.html')}`)

这次的文件夹是正确的:

console.log(path.join(__dirname,'../renderer/main_window/index.html')) =>
/home/raphy/ForgeTypescriptReactWebpack
/out/forgetypescriptreactwebpack-linux-x64/resources/app/.webpack
/renderer/main_window/index.html

raphy@raohy:~/ForgeTypescriptReactWebpack/out/forgetypescriptreactwebpack-linux-x64/resources/app/.webpack/renderer/main_window$ ls -lah
total 3,5M
drwxrwxr-x 2 raphy raphy 4,0K gen  4 12:17 .
drwxrwxr-x 3 raphy raphy 4,0K gen  4 12:17 ..
-rw-rw-r-- 1 raphy raphy  386 gen  4 12:17 index.html
-rw-rw-r-- 1 raphy raphy 3,4M gen  4 12:17 index.js
-rw-rw-r-- 1 raphy raphy 1,8K gen  4 12:17 preload.js

但是,页面仍然没有正确加载,也没有得到正确的输出

htrmnn0y

htrmnn0y1#

似乎是您加载页面的代码导致了此问题,它应该是:

// and load the index.html of the app.
mainWindow.loadURL(isDev ? MAIN_WINDOW_WEBPACK_ENTRY : `file://${path.join(__dirname, '../build/index.html')}`)

也就是说,三元运算符应该确定加载哪个路径/URL作为mainWindow.loadURL函数调用的参数。

jfgube3f

jfgube3f2#

感谢electron-forge的人,他们在Discord中回答了我的帮助请求,我通过以下两个步骤解决了这个问题:
1.我已将所有@electron-forge/* 依赖项升级到最新版本
1.在main中:主窗口。加载URL(主窗口网页备份条目)

相关问题