electron 退出程序
时间: 2023-10-01 16:03:49 浏览: 39
在 Electron 中,你可以使用 `app.quit()` 方法来退出程序。例如,当用户点击窗口的关闭按钮时,你可以监听 `window.on('close')` 事件,然后调用 `app.quit()` 方法来退出程序。以下是一个示例代码:
```javascript
const { app, BrowserWindow } = require('electron')
let mainWindow
function createWindow() {
mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
nodeIntegration: true
}
})
mainWindow.loadFile('index.html')
mainWindow.on('close', function(event) {
event.preventDefault()
mainWindow.hide()
// Do any cleanup here before quitting the app
app.quit()
})
}
app.on('ready', createWindow)
```
在上面的示例中,我们监听了窗口的 `close` 事件,并且调用了 `event.preventDefault()` 方法来阻止窗口的默认行为(即关闭窗口)。然后,我们调用了 `mainWindow.hide()` 方法来隐藏窗口,并且在退出程序之前可以做一些清理工作。最后,我们调用了 `app.quit()` 方法来退出程序。