来自Angular2内部的节点和电子模块

问题描述:

我正在开发电子应用程序使用Angular2来自Angular2内部的节点和电子模块

在我引用/电子的main.js装载NG应用程序:

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部件内部可用?

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

+0

不幸的是我甚至不能访问'electron'模块加载从'remote':'从“电子”进口{}远程;'抛出:'找不到名称' electron'' – cocoseis

如果这仍是有关 -

我不知道这个“官方”的方式呢。但也有变通的解决方案 - 主要是围绕需要index.html中电子/其他模块和访问window['electron'] -

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

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

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

你可以看到这个Here

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