electron-builder 编译后显示白页

electron-builder shows white page after compile

我正在尝试构建一个 Electron 应用程序。为此,我使用以下 respos:
https://github.com/electron/electron-quick-start
https://github.com/electron-userland/electron-builder

在开发模式下(电子。)一切正常。但是当我构建应用程序并启动它时,它只显示一个空白页面,开发控制台或构建日志中没有任何错误。

为什么它在生产环境中不起作用?我所有的文件都在一个方向:

我在基础 main.js 文件中没有做太多改动:

  // Modules to control application life and create native browser window
const {app, BrowserWindow} = require('electron')
const path = require('path')
const url = require('url')

function createWindow () {
  // Create the browser window.
  const mainWindow = new BrowserWindow({
    width: 800,
    height: 600,
    frame: false,
    webPreferences: {
      preload: path.join(__dirname, 'preload.js'),
      nodeIntegration: true,
      webSecurity: false
    }
  })

  // and load the index.html of the app.
  //mainWindow.loadFile('index.html')
  mainWindow.loadURL(url.format({
    pathname: path.join(__dirname, 'index.html'),
    protocol: 'file:',
    slashes: true
  }));

  // Open the DevTools.
  mainWindow.webContents.openDevTools();

}

// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.whenReady().then(createWindow)
// Quit when all windows are closed.
app.on('window-all-closed', function () {
  // On macOS it is common for applications and their menu bar
  // to stay active until the user quits explicitly with Cmd + Q
  if (process.platform !== 'darwin') app.quit()
})

app.on('activate', function () {
  // On macOS it's common to re-create a window in the app when the
  // dock icon is clicked and there are no other windows open.
  if (BrowserWindow.getAllWindows().length === 0) createWindow()
})

// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.

那是因为你在生产中的输出 html 应该位于其他地方,顺便说一句,我正在使用 angular 和 electron,输出在 dist 文件夹中,但我不使用 url.format:尝试 mainWindow.loadURL(`file://$_dirname/index.html`) 或者如果你有 dist 文件夹 mainWindow.loadURL(`file://$_dirname/dist/index.html') 也尝试改变 app.whenReady() 来自 app.on("ready",createWindow) 希望以后有用