安装
npm install vue-cookies --save
引入
import Vue from 'vue'
import VueCookies from 'vue-cookies'
Vue.use(VueCookies)
使用
window.iSoftCookiesKey = ['ISoftLoginUserInfo-Test', 'ISoftElectronicsProcessJWT-Test']
//存储cookies
that.$cookies.config('1d') //1天后过期
that.$cookies.set(window.iSoftCookiesKey[0], {
"UserCode": _resData.domainAccount,
"ExpTime": _resData.tokenExpiredDate,
"DisplayName": _resData.realName + ' ' + _resData.domainAccount
}, null, null, "h3c.com")
that.$cookies.set(window.iSoftCookiesKey[1], _resData.jwtToken)
that.$cookies.set(window.iSoftCookiesKey[1], _resData.jwtToken, null, null, "h3c.com")
//是否存在某key
that.$cookies.isKey(window.iSoftCookiesKey[0]) // true/false
//删除cookies
that.$cookies.remove(window.iSoftCookiesKey[0], null, "h3c.com")
that.$cookies.remove(window.iSoftCookiesKey[1], null, "h3c.com")
document.cookie
//存储
documents.cookie = 'cookieName=' + escape('cookievalue') + ';expires=' + expirationDateObj.toGMTString();
//获取
function getCookie(cookieName) {
var cookieString = documents.cookie;
var start = cookieString.indexOf(cookieName + '=');
// 加上等号的原因是避免在某些 Cookie 的值里有
// 与 cookieName 一样的字符串。
if (start == -1) // 找不到
return null;
start += cookieName.length + 1;
var end = cookieString.indexOf(';', start);
if (end == -1)
return unescape(cookieString.substring(start));
return unescape(cookieString.substring(start, end));
}