3

I'm building an simple stand-alone angular application in which u can submit information. This information needs to be saved in a local JSON file that's in the asset folder. I know Angular runs in a web browser that's why I use electron to build it. The problem is that i can't figure out a way to edit the JSON files in angular 5 using electron (local).

I have tried the solutions mentioned in this post, But they didn't work for me, any other solutions?

Jelle
  • 758
  • 2
  • 14
  • 36

2 Answers2

4

After having this problem for quite some time i finally figured out how to solve it:

you need to put this in script tags in your index.html

  var remote = require('electron').remote
  var fs = remote.require('fs');

and in every component you want to use it you need to declare it globally

declare var fs: any;

then you can use it!

was quite a struggel to figure it out...

Nestoro
  • 787
  • 6
  • 17
1

Because it's just JSON data, might I suggest using localStorage instead? Then, you can do something like:

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

;

Later, when you need to load the JSON to edit it or read it, just use:

jsonObj = JSON.parse(window.localStorage.getItem('mysavedJSON');
Wayne F. Kaskie
  • 3,257
  • 5
  • 33
  • 43