sanallen 2019-06-30
Electron不支持之前在浏览器中使用localstroage的方法,暂时采用cookie解决。
官方文档
https://electronjs.org/docs/a...参考博客
https://blog.csdn.net/changhu...const Cookie = require('./static/js/cookie.js') Cookie.getCookies('token').then(function (res) { console.log('res', res) })
// cookie.js // 通过remote模块取的原本在主进程中才能使用的session模块 const session = require('electron').remote.session; /** * 获得 */ const Cookie = {} const Session = session Cookie.getCookies = (name) => { let data = [] return new Promise(function (resolve, reject) { Session.defaultSession.cookies.get({ name:name}, function (error, cookies) { console.log('cookies', cookies); if (cookies.length > 0) { data = cookies } resolve(cookies) }) }) }; /** * 清空缓存 */ Cookie.clearCookies = (url) => { Session.defaultSession.clearStorageData({ origin: url, storages: ['cookies'] }, function (error) { if (error) console.error(error); }) }; /** * 保存cookie * @param name cookie名称 * @param value cookie值 */ Cookie.setCookie = (name, value) => { let Days = 30; let exp = new Date(); let date = Math.round(exp.getTime() / 1000) + Days * 24 * 60 * 60; const cookie = { url: "", name: name, value: value, expirationDate: date }; Session.defaultSession.cookies.set(cookie, (error) => { if (error) console.error(error); }); }; // export default cookie module.exports = Cookie