在Angular 2和Webpack中加载电子模块

时间:2016-12-08 15:40:08

标签: javascript node.js angular typescript electron

我正在使用this starter kit来创建电子申请。当我尝试将电子模块导入Angular 2组件时,如下所示:

const electron = require('electron');

我从入门套件中的此文件中收到此错误fs.readFileSync is not a function

var fs = require('fs')
var path = require('path')

module.exports = path.join(__dirname, fs.readFileSync(path.join(__dirname, 'path.txt'), 'utf-8'))

当我在组件中不使用require时,一切正常。这就像使用require更改其他地方使用的require类型,但我真的不知道发生了什么。我尝试过将电子模块导入角度分量的其他方法,但我得到了:

Cannot find module 'electron'.

1 个答案:

答案 0 :(得分:0)

电子需要NodeJS,浏览器只能访问服务器所能访问的文件系统。

但Electron使用node渲染index.html文件,因此使用以下内容将与Angular应用程序一起使用。

在Angular的index.html文件中,您应该放置

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

使用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>