繁体   English   中英

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

[英]Load electron modules in Angular 2 and Webpack

我正在使用这个入门工具包来创建电子应用程序。 当我尝试将电子模块导入这样的Angular 2组件中时:

const electron = require('electron');

我收到此错误fs.readFileSync is not a function入门工具包中此文件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'.

Electron要求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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM