vue記住密碼功能
阿新 • • 發佈:2018-12-27
話不多說,直接上程式碼。
html部分:
JS部分:
data() {
return {
logining: false,
ruleForm2: {
account: '',
checkPass: ''
},
rules2: {
account: [
{required: true, message: '請輸入賬號', trigger: 'blur'},
],
checkPass: [
{required: true, message: '請輸入密碼', trigger: 'blur'},
]
},
checked: true,
msg:''
};
},
methods: {
handleSubmit2(formName) {
let self = this;
//判斷複選框是否被勾選 勾選則呼叫配置cookie方法
if (self.checked == true) {
//傳入賬號名,密碼,和儲存天數3個引數
self.setCookie(self.ruleForm2.account, self.ruleForm2.checkPass, 7);
}else {
//清空Cookie
self.clearCookie();
}
//設定cookie setCookie(c_name, c_pwd, exdays) { var exdate = new Date(); //獲取時間 exdate.setTime(exdate.getTime() + 24 * 60 * 60 * 1000 * exdays); //儲存的天數 //字串拼接cookie window.document.cookie = "userName" + "=" + c_name + ";path=/;expires=" + exdate.toGMTString(); window.document.cookie = "userPwd" + "=" + c_pwd + ";path=/;expires=" + exdate.toGMTString(); }, //讀取cookie getCookie: function() { if (document.cookie.length > 0) { var arr = document.cookie.split('; '); //這裡顯示的格式需要切割一下自己可輸出看下 for (var i = 0; i < arr.length; i++) { var arr2 = arr[i].split('='); //再次切割 //判斷查詢相對應的值 if (arr2[0] == 'userName') { this.ruleForm2.account = arr2[1]; //儲存到儲存資料的地方 } else if (arr2[0] == 'userPwd') { this.ruleForm2.checkPass = arr2[1]; } } } }, //清除cookie clearCookie: function() { this.setCookie("", "", -1); //修改2值都為空,天數為負1天就好了 } }, mounted(){ this.getCookie(); } }