通过网站关闭申请

Close application through the website

我想我在 Electron 应用程序的构建方式中遗漏了一些东西,但我在我的 main.js 中使用无框架选项来隐藏我的应用程序的退出按钮,我想在代码,但我不知道如何触发应用程序的实际终止。我正在使用与 Electron 结合的 angular2 CLI 生成器项目。

根据我的谷歌搜索,我发现我需要使用 const remote = require('electron').remote; 但这给了我一个对我来说毫无意义的 index.js:4Uncaught TypeError: fs.readFileSync is not a function 错误。我相信这是因为我正在尝试在 Angular 应用程序上执行仅节点模块。 我想我错过了什么。

有人知道这是怎么回事吗?

main进程中,可以调用app.quit()关闭应用,其中app来自require('electron').

如果您想从 render 进程中关闭应用程序,您可以让退出按钮将 IPC 消息发送回 main通过做类似

的事情来处理
const {ipcRenderer} = require('electron');
ipcRenderer.send('exit-message', 'close');

这将广播到 main 在那里你可以适当地处理它,就像这样

ipcMain.on('exit-message', (event, arg) => {
  app.quit();
  // or handle the actual message if you need other functionality
})

问题是 angular CLI 使用 commonJS,而我试图使用 require,但效果不佳。为了解决这个问题,我在 index.html

中添加了以下内容
  <script>
    const electron = require('electron');
  </script>

然后在我的 typings.d.ts 中我添加了 declare var electron: any; 这样当我尝试在整个应用程序中访问 electron 时它就不会抛出错误。