electron使用动态配置文件及持久化存储

1.如何在打包之后,把动态配置文件比如【config.json】放在根目录,不被打包到asar文件中

//解决思路,electron可以拷贝静态资源,比如你把config.json放在项目的根目录下,打包时候打包到EXE根目录下即可。
"build": {
    "productName": "machine-electron-project",
    "appId": "com.example.yourapp",
    "extraResources":  {
      "from": "./config.json",
      "to": "../"
    }
   }
//当然如果你想把配置文件放在其他地方也是可以的,在vue中使用 fs.readFile也能读取到,比如config.json文件在d盘下,你可以把文件路径写成'd:config.json'

2.electron使用持久化存储electron-store,在主进程中使用app.getPath('userData')是没问题的,但是在渲染进程中使用是undifind

//渲染进程使用主进程的方法使用remote
const { app,remote } = require('electron')
//持久化存储
const Store = require('electron-store');
function initStore(){
    let option={
        name:"userInfo",//文件名称,默认 config
        fileExtension:"json",//文件后缀,默认json
        cwd:remote.app.getPath('userData'),//文件位置,尽量不要动
    //    encryptionKey:"aes-256-cbc" ,//对配置文件进行加密
        clearInvalidConfig:true, // 发生 SyntaxError  则清空配置,
    } 
    Vue.prototype.$electronStore = new Store(option);
}
export default {
    initStore
}

3.下面关于electron-vue打包时的配置

{
  "name": "demo",
  "version": "0.0.2",
  "author": "<1392293229@qq.com>",
  "build": {  // electron-builder配置
    "productName":"xxxx",//项目名 这也是生成的exe文件的前缀名
    "appId": "xxxxx",//包名  
    "copyright":"xxxx",//版权  信息
    "compression": "store", // "store" | "normal"| "maximum" 打包压缩情况(store 相对较快),store 39749kb, maximum 39186kb
    "directories": {
        "output": "build" // 输出文件夹
    }, 
    "asar": false, // asar打包
    "extraResources":  { // 拷贝dll等静态文件到指定位置
        "from": "./app-update.yml",
        "to": "./b.txt"
    },
    "win": {  
        "icon": "xxx/icon.ico"//图标路径,
        "extraResources":  { // 拷贝dll等静态文件到指定位置(用于某个系统配置)
            "from": "./app-update.yml",
            "to": "./b.txt"
        }
    },
    "nsis": {
        "oneClick": false, // 一键安装
        "guid": "xxxx", //注册表名字,不推荐修改
        "perMachine": true, // 是否开启安装时权限限制(此电脑或当前用户)
        "allowElevation": true, // 允许请求提升。 如果为false,则用户必须使用提升的权限重新启动安装程序。
        "allowToChangeInstallationDirectory": true, // 允许修改安装目录
        "installerIcon": "./build/icons/aaa.ico", // 安装图标
        "uninstallerIcon": "./build/icons/bbb.ico", //卸载图标
        "installerHeaderIcon": "./build/icons/aaa.ico", // 安装时头部图标
        "createDesktopShortcut": true, // 创建桌面图标
        "createStartMenuShortcut": true, // 创建开始菜单图标
        "shortcutName": "xxxx" // 图标名称
    }
  }
}
原文地址:https://www.cnblogs.com/lizhao123/p/14355242.html