在 Angular 5 中访问文件系统,使用 electron

Accessing filesystem in Angular 5, using electron

我正在构建一个简单的独立 angular 应用程序,您可以在其中提交信息。 此信息需要保存在资产文件夹中的本地 JSON 文件中。我知道 Angular 在网络浏览器中运行,这就是我使用电子来构建它的原因。问题是我无法找到使用电子(本地)编辑 angular 5 中的 JSON 文件的方法。

我已经尝试了中提到的解决方案,但它们对我不起作用,还有其他解决方案吗?

因为它只是 JSON 数据,我可以建议改用 localStorage 吗?然后,您可以执行以下操作:

...// Code that creates the JSON object
var theJSONdata = jsonObj.stringify(); // conver the object to a string
window.localStorage.setItem('mysavedJSON', theJSONdata)

;

以后需要加载JSON进行编辑或阅读时,只需使用:

jsonObj = JSON.parse(window.localStorage.getItem('mysavedJSON');

在遇到这个问题一段时间后,我终于想出了解决方法:

您需要将其放入 index.html

的脚本标签中
  var remote = require('electron').remote
  var fs = remote.require('fs');

并且在每个要使用它的组件中都需要全局声明它

declare var fs: any;

那你就可以用了!

很难弄清楚...