【问题标题】:Electron index.html not loading after building the app构建应用程序后未加载电子 index.html
【发布时间】:2020-01-09 22:57:33
【问题描述】:

在与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

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

【问题讨论】:

    标签: javascript electron electron-builder


    【解决方案1】:

    看来我的问题毕竟是由于包装不当造成的。在package.json "build" 配置中添加这个修复了它:

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

    【讨论】:

      【解决方案2】:

      检查一次 index.html 的目录路径 我认为您遵循任何教程并且不验证您的路径一次检查我希望这将解决您的问题。

      路径名:path.join(__dirname, ./src/index.html),

      【讨论】:

        猜你喜欢
        • 2020-12-31
        • 2023-02-05
        • 1970-01-01
        • 2021-09-19
        • 2018-07-15
        • 1970-01-01
        • 2021-03-20
        • 2015-03-06
        • 2022-12-04
        相关资源
        最近更新 更多