2017-01-23 47 views
1

我正在開發電子應用程序使用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部件內部可用?

回答

0

您無法直接從Angular內部調用Electron/Node模塊。相反,檢查出the Electron remote API

+0

不幸的是我甚至不能訪問'electron'模塊加載從'remote':'從「電子」進口{}遠程;'拋出:'找不到名稱' electron'' – cocoseis

1

如果這仍是有關 -

我不知道這個「官方」的方式呢。但也有變通的解決方案 - 主要是圍繞需要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*)存儲模塊