来自Angular2内部的Node和Electron的模块

时间:2017-01-23 14:48:52

标签: node.js angular typescript electron

我正在使用 Angular2 开发 Electron 应用程序。

在电子的main.js我正在引用/加载NG App:

const {app, BrowserWindow} = require('electron')
const path = require('path')
const url = require('url')

let win

function createWindow () {
  win = new BrowserWindow({width: 800, height: 600})

  // load the index.html of the NG app:
  win.loadURL(url.format({
    pathname: path.join(__dirname, '/../../dist/index.html'),
    protocol: 'file:',
    slashes: true
  }))

[...]

这就像一个魅力。但是,我现在想从NG部分中访问节点和电子模块

当我尝试导入例如:fs模块时:

import * as fs from "fs";

它仍在编译,但只要我打电话给fs.readFile(...),它就说:

__WEBPACK_IMPORTED_MODULE_2_fs__.readFile is not a function

当我考虑它时,由于模块不在node_modules文件夹内(右边?),因此不能也无法工作。 我需要做些什么才能在NG部分内部使用它们?

2 个答案:

答案 0 :(得分:1)

如果这仍然相关 -

我还不知道这种“正式”方式。但是有一些解决方案 - 主要是在index.html中需要电子/其他模块并访问window['electron'] -

<script>
    window.electron = require('electron');
</script>

或创建访问电子对象的角度服务。

declare const window: ElectronWindow;
export class ChildProcessService {...}

您可以看到此Here

的实现
  • ElectronWindow指的是您可以创建的自定义界面,并添加一个require()函数来处理打字。
  • 使用window.require(*some-node-module*)存储模块

答案 1 :(得分:0)

您无法直接从Angular内部调用Electron / Node模块。相反,请查看the Electron remote API

相关问题