我不能在我的渲染器进程中要求内部模块。 当我需要内部节点模块甚至 Electron 时,我得到一个错误或未定义或空对象。
例如:
import * as fs from 'fs';
console.log(fs) // empty object
import { spawn } from 'child_process'; // Can't find child_process module
import * as electron from 'electron' // fs.readFileSync is not a function
这是我的代码: Electron
import { app, BrowserWindow } from 'electron';
let mainWin = null;
const loadURL = `http://localhost:4200`;
const createWindow = () => {
mainWin = new BrowserWindow({
width: 800,
height: 800
});
mainWin.loadURL(loadURL);
mainWin.on('closed', () => {
mainWin = null;
});
}
app.on('ready', createWindow);
app.on('activate', () => {
if (!mainWin) {
createWindow();
}
});
渲染器处理 Angular 代码:
import { Component } from '@angular/core';
import { readdirSync } from 'fs';
import { spawn } from 'child_process';
console.log(readdirSync);
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.styl']
})
export class AppComponent {
constructor() {}
}
我做错了什么?
最佳答案
您在用于浏览器的 Angular 中包含服务器端模块。
Electron 需要 NodeJS,浏览器无法访问文件系统,只有服务器可以。
但是 Electron 使用 node 渲染 index.html 文件,所以你可以通过 index.html 文件请求 electron
在 Angular 应用中使用 Electron 部分的示例。
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Angular App</title>
<base href="./">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" type="image/x-icon" href="favicon.ico">
</head>
<body>
<app-root></app-root>
<script>
window.electron = require("electron");
window.ipcRenderer = require("electron").ipcRenderer;
window.electronRemote = require("electron").remote;
</script>
</body>
</html>
关于angular - Electron Angular2(angular-cli)要求内部模块失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40452564/