我有一个电子应用程序,在与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
有什么想法吗?预先感谢!
答案 0 :(得分:0)
所以,看来我的问题毕竟是由于包装不当引起的。在package.json
“构建”配置中添加它可以解决此问题:
"directories": {
"buildResources": "build",
"app": "build"
}
答案 1 :(得分:0)
检查一次 index.html 的目录路径 我认为您按照任何教程进行操作,并且不会仅在检查一次后验证您的路径,我希望这会解决您的问题。
路径名:path.join(__dirname, ./src/index.html
),