Node.js 节点';s和电子';s模块从内部角度2
我正在使用Angular2开发一个Electron应用程序 在electron的Node.js 节点';s和电子';s模块从内部角度2,node.js,angular,typescript,electron,Node.js,Angular,Typescript,Electron,我正在使用Angular2开发一个Electron应用程序 在electron的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, heig
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部件内部访问node和electron的模块
当我尝试导入时,例如:fs
模块,如:
import * as fs from "fs";
它仍然可以编译,但每当我调用fs.readFile(…)
时,它都会显示:
__WEBPACK_IMPORTED_MODULE_2_fs__.readFile is not a function
仔细想想,这不起作用,也不可能起作用,因为模块不在node\u modules
文件夹中(对吗?)。
我需要做什么才能使它们在NG部件内部可用?您不能直接从内部调用电子/节点模块。相反,请检查。以防这仍然相关- 我还不知道有什么“官方”的方法来解决这个问题。但也有一些变通解决方案——主要是在index.html中要求使用electron/其他模块和访问
窗口['electron']
-
<script>
window.electron = require('electron');
</script>
你可以看到这个的一个实现
指的是一个自定义界面,您可以创建并添加require()函数来处理打字ElectronWindow
- 使用
来存储模块窗口。需要(*某些节点模块*)
electron
模块来从以下位置加载remote
:import{remote}from“electron”代码>抛出:找不到名称“electron”
declare const window: ElectronWindow;
export class ChildProcessService {...}