在electron主進程中使用localstorage

electron主進程中是不能獲取到瀏覽器的window對象的,所以我們不能像在渲染進程中一樣使用瀏覽器爲我們提供的localstorage對象。

但是主進程中有可能也需要這樣的需求,比如我們在本地存儲了當前的環境(dev/beta/prod),主進程需要根據不同的開發環境來load不同的url。

於是手動封裝了一個可以在主進程中調用的localstorage。

1.安裝

npm install electron-localStorage

2.引用:

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

3.使用

3.1完美支持所有localStorage的所有api:

存儲數據

storage.setItem(`myCat`, `Tom`);

獲取數據

let cat = 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/electron-localStorage

5.示例程序

https://github.com/ConardLi/electron-localstorage-demo

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章