我试图提示“打开文件”对话框,并在electron docs中找到了dialog.showOpenDialog(...)
。
我现在将该行添加到了几个不同的项目中,结果该行一经崩溃便崩溃了。我会看到文件浏览器对话框窗口打开了一秒钟,然后崩溃了。控制台没有显示任何错误消息,它显示的最后一句话是Promise { <pending> }
。
我尝试过的事情:
dialog.showOpenDialog(...)
,以查看是否发现任何错误。一旦到达该行,它总是崩溃,没有错误消息。npm install electron --save-dev
删除我的node_modules文件夹并重新安装electronic electron .
npm uninstall -g electron
和本地npm start
卸载全局电子--disable-gpu
参数我正在使用Ubuntu 19.04,而不是VM,但是我也在Ubuntu 18.04的VM中对其进行了测试。
一个有趣的注意事项是,当我尝试使用带有type = file
的html表单按钮来完成同一任务时,在打开文件浏览器对话框后,它也崩溃了电子应用程序。但是,当我在常规的网络浏览器中打开html页面时,它确实起作用了。
这是我的main.js
const { app, BrowserWindow } = require('electron')
// 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
// 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.
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.loadFile('index.html')
// Open the DevTools.
win.webContents.openDevTools()
// 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
})
const { dialog } = require('electron')
console.log(dialog.showOpenDialog({ properties: ['openFile', 'multiSelections'] })) //*****THIS LINE HERE
}
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.
这是我的package.json
{
"name": "electron-demo",
"version": "1.0.0",
"description": "",
"main": "main.js",
"scripts": {
"start": "electron .",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"devDependencies": {
"electron": "^7.1.2"
}
}