Vue+Electron cookies設置


在網頁版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"

這個問題,目前還在找解決方案


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM