Electron Builder:不允许加载本地资源:app.asar/build/index.html

Electron Builder: Not allowed to load local resource: app.asar/build/index.html

我在使用 electron builder 时遇到问题我在控制台中出现空白页面和错误:

Not allowed to load local resource: file:///C:/Users/emretekince/Desktop/DCSLogBook/client/dist/win-unpacked/resources/app.asar/build/index.html

main.js

const startUrl = process.env.ELECTRON_START_URL || url.format({
        pathname: path.join(__dirname, '/build/index.html'),
        protocol: 'file:',
        slashes: true
    });
mainWindow.loadURL(startUrl);

通过在package.json

中添加"files"解决
"files": [
  "*.js",
  "build",
  "node_modules"
],

我遇到了同样的问题,并设法解决了这个问题:

path.resolve('index.html')

像这样:

const startUrl = path.resolve('index.html'); mainWindow.loadURL(startUrl);

我也遇到了同样的问题,我在加载文件之前放置了下面一行。

 window.webContents.openDevTools()

示例代码

// Issue code
window =  new BrowserWindow({width:800,height:600,parent:mainWindow})
window.webContents.openDevTools()
window.loadURL(url.format({
    pathname: path.join(__dirname,'/../views/file.html'),
    protocol: 'file',
    slashes: true
}))

// Issue Solved code
window =  new BrowserWindow({width:800,height:600,parent:mainWindow})

window.loadURL(url.format({
    pathname: path.join(__dirname,'/../views/file.html'),
    protocol: 'file',
    slashes: true
}))
window.webContents.openDevTools()

我认为您的 index.html 文件不在您给定的位置。 __dirname, '/build/index.html'

我错过了这个愚蠢的点,浪费了很多时间。 Angular-cli 在 dist.

的文件夹内为 index.html 创建默认位置
dist/project-name/index.html

我一整天都在努力解决这个问题,终于找到了解决方案,

"build": {
"appId": "myledgerapp",
"extends": null,
"files": [
  "./build/**/*",
  "./public/electron.js"
]}

我们需要在构建部分添加文件,其中 electron.js 是我的入口点。

我遇到了类似的问题,但缺少 path.join:

错误代码:

win.loadFile('index.html')

固定代码:

win.loadFile(path.join(__dirname, 'index.html'))