How to persist data in an Electron app?

前端 未结 9 1132
刺人心
刺人心 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:21

    There is a module that gives simple methods to get and set json files to this directory, creates subdirectories if needed and supports callbacks and promises:

    https://github.com/ran-y/electron-storage

    Readme:

    Installation

    $ npm install --save electron-storage
    

    usage

    const storage = require('electron-storage');
    

    API

    storage.get(filePath, cb)

    storage.get(filePath, (err, data) => {
      if (err) {
        console.error(err)
      } else {
        console.log(data);
      }
    });
    

    storage.get(filePath)

    storage.get(filePath)
    .then(data => {
      console.log(data);
    })
    .catch(err => {
      console.error(err);
    });
    

    storage.set(filePath, data, cb)

    storage.set(filePath, data, (err) => {
      if (err) {
        console.error(err)
      }
    });
    

    storage.set(filePath, data)

    storage.set(filePath, data)
    .then(data => {
      console.log(data);
    })
    .catch(err => {
      console.error(err);
    });
    

    storage.isPathExists(path, cb)

    storage.isPathExists(path, (itDoes) => {
      if (itDoes) {
        console.log('pathDoesExists !')
      }
    });
    

    storage.isPathExists(path)

    storage.isPathExists(path)
    .then(itDoes => {
      if (itDoes) {
        console.log('pathDoesExists !')
      }
    });
    

提交回复
热议问题