【发布时间】:2021-05-10 22:39:37
【问题描述】:
我正在尝试弄清楚如何关闭带有角度组件的电子应用程序。我通过在 BrowserWindow({... inside main.js 中设置 frame: false 来删除菜单栏。我在电子应用程序的右上角有一个来自组件的关闭按钮。我希望能够从点击时的component.ts文件,但我还没有看到任何从角度组件关闭电子的示例。
我认为以下方法会起作用,但没有。我正在从 main.js 导出一个函数并从组件中调用该函数。
像这样(见closeApp()):
const { app, BrowserWindow } = require('electron')
const url = require("url");
const path = require("path");
let mainWindow
function createWindow() {
mainWindow = new BrowserWindow({
width: 800,
height: 600,
frame: false,
webPreferences: {
nodeIntegration: true
}
})
mainWindow.maximize();
mainWindow.loadURL(
url.format({
pathname: path.join(__dirname, `/dist/index.html`),
protocol: "file:",
slashes: true
})
);
...
function closeApp() {
mainWindow = null
}
export { closeApp };
然后我会尝试将其导入到组件中
import { Component, OnInit } from '@angular/core';
import { closeApp } from '../../../main.js';
@Component({
selector: 'app-header',
templateUrl: './header.component.html',
styleUrls: ['./header.component.scss']
})
export class HeaderComponent implements OnInit {
constructor() { }
ngOnInit(): void {
}
testClose() {
closeApp();
}
}
如何从角度关闭电子应用程序?感谢您的帮助!
【问题讨论】:
-
I get build errors with electron...你能分享这些错误吗? -
更正:我确实有构建错误,但它们无关。构建错误现在已经消失,所以这不是问题。
-
我认为这个问题的答案仍然适用:stackoverflow.com/questions/43314039/…
-
如果我尝试这个解决方案:stackoverflow.com/a/43314199/4350389 然后我得到编译器错误。这是该解决方案中我更新的更改和编译器错误。 pastebin.com/yXLqaKXz你知道如何解决这些错误吗?
-
不能使用IPC调用吗?我通常有一个 Angular 服务来完成与 Node 进程的所有通信。您可以查看this article 以获得更多解释