首页 > 解决方案 > 构建应用程序后未加载电子 index.html

问题描述

我有一个电子应用程序,在与electron-builder. 捆绑它并打开应用程序后,我收到以下错误:

Not allowed to load local resource: file:///tmp/.mount_displa4VwuQh/resources/app.asar/file:/tmp/.mount_displa4VwuQh/resources/app.asar/build/index.html

在构建文件夹中,我有electron.js文件,index.html并且由于应用程序正在启动electron.js并因此index.html被正确捆绑。

这是我的electron.js应用程序入口点(基本上是样板文件):

const { app, BrowserWindow } = require('electron')
const url = require('url')
const path = require('path')

// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let win

function createWindow() {
    // Create the browser window.
    win = new BrowserWindow({
        width: 800,
        height: 600,
        webPreferences: {
            nodeIntegration: true
        }
    })


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

    // Emitted when the window is closed.
    win.on('closed', () => {
        // Dereference the window object, usually you would store windows
        // in an array if your app supports multi windows, this is the time
        // when you should delete the corresponding element.
        win = null
    })
}

// 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.on('ready', createWindow)

// Quit when all windows are closed.
app.on('window-all-closed', () => {
    // 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', () => {
    // 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 (win === null) {
        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.

当尝试使用loadFile()而不是loadUrl()我得到相同的错误,但使用此路径时:file:///tmp/.mount_displa4VwuQh/resources/app.asar/index.html

知道有什么问题吗?提前致谢!

标签: javascriptelectronelectron-builder

解决方案


所以看起来我的问题毕竟是由于包装不当造成的。在package.json“构建”配置中添加它修复了它:

"directories": {
  "buildResources": "build",
  "app": "build"
}

推荐阅读