2016-11-06 8 views
2

レンダラープロセスで内部モジュールを要求することはできません。 私は内部ノードモジュールまたは電子を必要としたいとき、私はエラーまたは未定義または空のオブジェクトを持っています。例えば電子Angular2(angular-cli)で内部モジュールの故障が発生する

:ここ

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 

は私のコードです: 電子

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(); 
    } 
}); 

レンダラプロセス角度コード:

私が間違ってやっている何
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() {} 
} 

答えて

0

ブラウザ用のサーバー側モジュールをAngularで指定しています。

ElectronではNodeJSが必要です。ブラウザはサーバがアクセスできるファイルシステムにアクセスできません。

しかし、あなたはあなたの角度アプリで電子の部品を使用してindex.htmlファイル

例を経由して電子を必要とすることができるので、電子は、ノードを使用してindex.htmlファイルをレンダリングします。

<!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> 
+0

これは良い習慣と考えられますか? – cocoseis

+0

私はあなたのビルドシステムに依存していると思いますが、いくつかは非常に制限されています。私はこれを、角度のあるcliビルドを使って電子を使って作業する最もバグのない方法だと見出しました。 –

+0

それには欠けている入力についてはどうですか? – cocoseis

関連する問題