当前位置: 首页 > 后端技术 > Node.js

在electron主进程中使用localstorage

时间:2023-04-03 13:24:13 Node.js

无法在electron主进程中获取到浏览器的window对象,所以我们不能像在渲染过程中那样使用浏览器提供的localstorage对象。但是主进程可能也需要这样的需求。比如我们将当前环境(dev/beta/prod)存储在本地,主进程需要根据不同的开发环境加载不同的url。于是手动封装了一个可以在主进程中调用的localstorage。1.安装npminstallelectron-localStorage2.参考:conststorage=require('electron-localStorage');3、使用3.1完美支持所有localStorageAPIs:storedatastorage.setItem(`myCat`,`Tom`);获取数据letcat=storage.getItem(`myCat`);删除某个数据storage.removeItem(`myCat`);删除所有数据storage.clear();3.2扩展方法获取当前所有存储项storage.getAll();自定义存储路径storage.setStoragePath(path.join(__dirname,'test.json'));获取当前数据存储路径storage.getStoragePath();4.源码下载https://github.com/ConardLi/e...5。示例程序https://github.com/ConardLi/e...