在网页版Vue中,我使用的是js-cookie这个包来实现cookie的
但是Electron中似乎不可以用,查询官方文档发现有一个cookies的类
于是在此类上重写cookie的get,set的实现
API参考:类:Cookies | Electron (electronjs.org)
const { session } = require('electron') export default class Cookies { getCookies(name){ const value = session.defaultSession.cookies.get({ url: 'http://www.voiceofhand.com', name: name}) return value } setCookies(name, value){ session.defaultSession.cookies.set({ url: 'http://www.voiceofhand.com', name: name, value: value}) } }
实际使用过程中,在 const { session } = require('electron') 会出现问题,
1、提示了:__dirname is not defined
解决方式:
webPreferences: { //支持完整node nodeIntegration:true, contextIsolation:false }
2、提示:Uncaught TypeError: fs.existsSync is not a function at getElectronPath
解决方式:修改为
const { session } = window.require('electron')
但是还是遇到了
[Vue warn]: Error in created hook: "TypeError: Cannot read property 'defaultSession' of undefined"
这个问题,目前还在找解决方案