【问题标题】:How to import electron in angular 5如何在角度5中导入电子
【发布时间】:2018-10-23 20:46:21
【问题描述】:

我正在尝试导入 electron 以从我的应用程序打印发票 使用

import { BrowserWindow } from 'electron'

但它正在抛出错误。

fs.existsSync 不是函数

另外,我曾尝试从 index.html 页面中要求它,例如

<script>  
        var electron = require('electron');  
</script>

但得到

require 未定义

【问题讨论】:

  • 错误的方式。您使用“angular”inside electron,这是一个允许“类似浏览器的代码”在其中运行的环境。而不是在“浏览器”中使用“它”。
  • 我想从我的应用打印发票,所以我已经导入 BrowserWindow
  • 请试试这个。它可能有效。 $ npm install electron --save-dev const {BrowserWindow} = require('electron')
  • @MdAlamin 我试过了,但是遇到Cannot find name 'require'. 错误
  • @sam 你的 angular cli 版本是什么??

标签: node.js angular requirejs electron


【解决方案1】:

目前我发现的最佳解决方案是使用ngx-electron

在你的电子中简单地做:

const { ipcMain } = require('electron');
ipcMain.on('ping', (event, arg) => {
    console.log('RECEIVED PING FROM ANGULAR APP', event, arg);
    event.sender.sendSync('pong', 'yeah yeah yeah');
});

在你的 Angular 应用中:

import { NgxElectronModule } from 'ngx-electron';

@NgModule({
    declarations: [AppComponent],
    imports: [
        ...
        NgxElectronModule
    ],
    ...
})

在你的组件内部:

import { ElectronService } from 'ngx-electron';
...
constructor(private _electronService: ElectronService)
...
if (this._electronService.isElectronApp) {
            this._electronService.ipcRenderer.on('pong', (event, arg) => {
                console.log(
                    'RECEIVED RESPONSE FROM ELECTRON TO ANGULAR APP',
                    event,
                    arg
                );
            });
        }
...
this._electronService.ipcRenderer.send(
            'ping',
            'wow wow wow'
        );

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-12
    • 1970-01-01
    • 2016-06-24
    • 2018-08-13
    • 2023-04-01
    相关资源
    最近更新 更多