Angular & Electron: ipcRenderer - __dirname 未定义

Angular & Electron: ipcRenderer - __dirname is not defined

我已经建立了一个新的 Angular 项目并安装了 electron。当我尝试在 angular 组件中使用 ipcRenderer 时,出现如下所示的错误 (ReferenceError)。

    Uncaught ReferenceError: __dirname is not defined
     at Object.vbkW (index.js:4)
     at __webpack_require__ (bootstrap:79)
     at Module.Sy1n (app.component.ts:2)
     at __webpack_require__ (bootstrap:79)
     at Module.ZAI4 (app.module.ts:2)
     at __webpack_require__ (bootstrap:79)
     at Module.zUnb (app-routing.module.ts:8)
     at __webpack_require__ (bootstrap:79)
     at Object.0 (main.js:10)
     at __webpack_require__ (bootstrap:79)

一些版本信息:

    Angular CLI: 11.2.14
    Node: 14.16.0
    @angular-devkit/architect       0.1102.14
    @angular-devkit/build-angular   0.1102.14
    @angular-devkit/core            11.2.14
    @angular-devkit/schematics      11.2.14
    @schematics/angular             11.2.14
    @schematics/update              0.1102.14
    rxjs                            6.6.7
    typescript                      4.1.5

这是我的 app.component.ts 代码,仅使用 ipcRenderer.send():

import { Component } from '@angular/core';
import { ipcRenderer } from 'electron';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})
export class AppComponent {
  title = 'portals-new-two';

  tempButton(): void {
    console.log('pressed!');
    ipcRenderer.send('open-dev-tools');
  }

}

虽然没有建议,但我已经尝试在我的 BroswerWindow 中设置 nodeIntegration: true 但仍然出现错误:

win = new BrowserWindow({ 
 width: 800,
 height: 600,
 webPreferences: {
  nodeIntegration: true,
 }
});

如果我删除 ipcRenderer.send('open-dev-tools'); 行,错误就会消失。

我在尝试使用 remote 而不是 ipcRenderer/ipcMain.

时也遇到了同样的错误

__dirname 也可以从我的 main.js 文件中正常登录到控制台。

如有任何帮助,我们将不胜感激。

好的,我没有找到发生这种情况的原因,但我使用这里的一个很棒的 Angular/Electron 入门项目解决了这个问题:

https://github.com/maximegris/angular-electron

这利用了一个 Angular 服务来连接所有的 electron 东西并且似乎运行良好。