1.下载插件
npm install --save js-base64
2.插件引入
const Base64 = require('js-base64').Base64
3.cookie设置函数
setUserInfo() { // 判断用户是否勾选记住密码,如果勾选,向cookie中储存登录信息, // 如果没有勾选,储存的信息为空 if (this.checked) { this.setCookie("account", this.loginForm.username); // base64加密密码 let passWord = Base64.encode(this.loginForm.password); this.setCookie("password", passWord); } else { this.setCookie("account", ""); this.setCookie("password", ""); } }, // 获取cookie getCookie(key) { if (document.cookie.length > 0) { var start = document.cookie.indexOf(key + "="); if (start !== -1) { start = start + key.length + 1; var end = document.cookie.indexOf(";", start); if (end === -1) end = document.cookie.length; return unescape(document.cookie.substring(start, end)); } } return ""; }, // 保存cookie setCookie(cName, value, expiredays) { var exdate = new Date(); exdate.setDate(exdate.getDate() + expiredays); document.cookie = cName + "=" + decodeURIComponent(value) + (expiredays == null ? "" : ";expires=" + exdate.toGMTString()); }
参考链接:https://www.cnblogs.com/qisi007/p/10200140.html