How to persist data in an Electron app?

前端 未结 9 1140
刺人心
刺人心 2021-01-30 07:00

I\'ve been scouring the Electron documentation to try and figure out how to persist data in an Electron app. For example, in iOS or OS X, you could use NSUserDefaults to store u

9条回答
  •  攒了一身酷
    2021-01-30 07:10

    There is an NPM module I wrote called electron-json-storage that is meant to abstract this out and provide a nice and easy interface to the developer.

    The module internally reads/writes JSON to/from app.getPath('userData'):

    const storage = require('electron-json-storage');
    
    // Write
    storage.set('foobar', { foo: 'bar' }).then(function() {
    
        // Read
        storage.get('foobar').then(function(object) {
            console.log(object.foo);
            // will print "bar"
        });
    
    });
    

提交回复
热议问题