我正在使用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
的类型,但我真的不知道发生了什么。我尝试过将电子模块导入angular组件的其他方法,但我得到了:
Cannot find module 'electron'.
发布于 2016-12-12 07:36:43
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>
https://stackoverflow.com/questions/41043257
复制相似问题