【问题标题】:Angular & Electron: ipcRenderer - __dirname is not defined [duplicate]Angular 和 Electron:ipcRenderer - __dirname 未定义 [重复]
【发布时间】:2021-08-29 01:46:14
【问题描述】:

我已经建立了一个新的 Angular 项目并安装了电子。当我尝试在 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 文件中登录到控制台。

任何帮助将不胜感激。

【问题讨论】:

标签: javascript angular typescript electron


【解决方案1】:

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

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

这利用了 Angular 服务来挂钩所有电子的东西,而且似乎工作得很好。

【讨论】:

    猜你喜欢
    • 2021-07-01
    • 2021-03-05
    • 2021-08-03
    • 1970-01-01
    • 2021-01-30
    • 1970-01-01
    • 2020-09-16
    • 1970-01-01
    • 2020-09-30
    相关资源
    最近更新 更多