使用Electron在Angular 2应用程序中访问文件系统 [英] Accessing filesystem in Angular 2 app using Electron

查看:236
本文介绍了使用Electron在Angular 2应用程序中访问文件系统的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道Angular 2是在无法访问文件系统的Web浏览器上运行的.

I know that Angular 2 is run on a web browser, which does not have access to the file system.

但是,我将Electron用作前端,并且还通过电子运行了该应用程序:

However, I'm using Electron as my front-end, and also running the app via electron:

"build-electron": "ng build --base-href . && cp src/electron/* dist",
"electron": "npm run build-electron && electron dist"

因此,我用npm run electron运行它,最后运行electron dist.

Therefore, I run it with npm run electron which at the very end runs electron dist.

因为我正在运行electron而不是ng,所以我认为我应该能够访问文件系统.但是,当我这样做时:

Since I'm running through electron and not ng I would think that I should be able to access the filesystem. However, when I do:

import * as fs from 'fs'

我收到一个错误:

ng:///AppModule/AppComponent_Host.ngfactory.js:5 ERROR TypeError: __WEBPACK_IMPORTED_MODULE_0_fs__.readFileSync is not a function(…)

类似地,当我尝试时:var fs = require('fs');

Similarly, when I try: var fs = require('fs');

我得到:

ng:///AppModule/AppComponent_Host.ngfactory.js:5 ERROR TypeError: fs.readFileSync is not a function

这是导致错误的呼叫:

this.config = ini.parse(fs.readFileSync('../../CONFIG.ini', 'utf-8'))

有人知道这是什么原因吗?

Does anyone have any idea what's causing this?

谢谢.

推荐答案

通过以下方式解决它:

1)弹出webpack:ng eject

1) Eject webpack: ng eject

2)将target: 'electron-renderer'添加到webpack.config.js

3)由于我们位于renderer中,因此需要远程,但fs仅在main process中可用(

3) Require remote, since we're in the renderer, but fs is only available in the main process (Read more): var remote = require('electron').remote;

4)Require fs(这次使用require的远程实现):var fs = remote.require('fs');

4) Require fs (this time using remotes implementation of require): var fs = remote.require('fs');

现在可以使用了!

这篇关于使用Electron在Angular 2应用程序中访问文件系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆