vue項目實現(xiàn)表單登錄頁保存賬號和密碼到cookie功能

實現(xiàn)功能:
1.一周內(nèi)自動登錄勾選時,將賬號和密碼保存到cookie,下次登陸自動顯示到表單內(nèi)
2.點擊忘記密碼則清空之前保存到cookie的值,下次登陸需要手動輸入
次要的就不說了直接上主要的代碼
html部分
<el-form :model="ruleForm" :rules="rules" ref="ruleForm" class="demo-ruleForm loginFrom">
<h1 style="font-size: 1.5rem;color: #fff;font-weight: bold;padding: 1rem 0;">登陸</h1>
<el-form-item >
<el-input placeholder="賬號" v-model="ruleForm.userName"></el-input>
</el-form-item>
<el-form-item>
<el-input type="password" v-model="ruleForm.password" placeholder="密碼"></el-input>
</el-form-item>
<div style="padding: 1rem 0 2rem 0;" class="clear">
<span class="lf" style="color:#0489cc;">幫助</span>
<div class="rt">
<el-checkbox v-model="checked" style="color:#a0a0a0;">一周內(nèi)自動登錄</el-checkbox>
<span @click="clearCookie" style="cursor: pointer;color: #f19149;font-size: 0.75rem;margin-left: 5px;">忘記密碼?</span>
</div>
</div>
<el-button type="primary" @click="submitForm('ruleForm')" style="width: 100%;">登陸</el-button>
</el-form>
js部分
data () {
return {
ruleForm: {
userName: '', //用戶名
password: '' //密碼
},
}
}
methods: {
//點擊登錄調(diào)用方法
submitForm(formName) {
//保存的賬號
var name=this.ruleForm.userName;
//保存的密碼
var pass=this.ruleForm.password;
if(name==''||name==null){
alert("請輸入正確的用戶名");
return
}else if(pass==''||pass==null) {
alert("請輸入正確的密碼");
return
}
//判斷復(fù)選框是否被勾選 勾選則調(diào)用配置cookie方法
if(this.checked=true){
//傳入賬號名,密碼,和保存天數(shù)3個參數(shù)
this.setCookie(name,pass,7);
}
//接口
var url='myserver/user/login';
this.$http.post(url,this.ruleForm,{emulateJSON:true})
.then(res=>{
if(res.body=="fail"){
alert("用戶名或密碼錯誤,請重新輸入");
this.ruleForm.userName='';
this.ruleForm.password='';
return
} else{
alert("登陸成功!")
this.$router.push("/index")
}
});
},
//設(shè)置cookie
setCookie(c_name,c_pwd,exdays) {
var exdate=new Date();//獲取時間
exdate.setTime(exdate.getTime() + 24*60*60*1000*exdays);//保存的天數(shù)
//字符串拼接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('=');//再次切割
//判斷查找相對應(yīng)的值
if(arr2[0]=='userName'){
this.ruleForm.userName=arr2[1];//保存到保存數(shù)據(jù)的地方
}else if(arr2[0]=='userPwd'){
this.ruleForm.password=arr2[1];
}
}
}
},
//清除cookie
clearCookie:function () {
this.setCookie("","",-1);//修改2值都為空,天數(shù)為負1天就好了
}
},
//頁面加載調(diào)用獲取cookie值
mounted(){
this.getCookie()
}

總結(jié)
以上所述是小編給大家介紹的vue實現(xiàn)表單登錄頁保存賬號和密碼到cookie功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
- Vue實現(xiàn)用戶沒有登陸時,訪問后自動跳轉(zhuǎn)登錄頁面的實現(xiàn)思路
- Vue項目如何保持用戶登錄狀態(tài)(localStorage+vuex刷新頁面后狀態(tài)依然保持)
- vue實現(xiàn)用戶長時間不操作自動退出登錄功能的實現(xiàn)代碼
- vue 實現(xiàn)用戶登錄方式的切換功能
- VuePress 中如何增加用戶登錄功能
- VUE:vuex 用戶登錄信息的數(shù)據(jù)寫入與獲取方式
- vue同一個瀏覽器登錄不同賬號數(shù)據(jù)覆蓋問題解決方案
- vue?+elementui?項目登錄通過不同賬號切換側(cè)邊欄菜單的顏色
- Vue實現(xiàn)登錄記住賬號密碼功能的思路與過程
- vue同一瀏覽器登錄多賬號處理方案
相關(guān)文章
vue使用axios實現(xiàn)文件上傳進度的實時更新詳解
最近在學習axios,然后項目就用到了,所以這篇文章主要給大家介紹了關(guān)于vue中利用axios實現(xiàn)文件上傳進度的實時更新的相關(guān)資料,文中先對axios進行了簡單的介紹,方法大家理解學習,需要的朋友們下面隨著小編來一起學習學習吧。2017-12-12
Vue2中Element?DatePicker組件設(shè)置默認日期及控制日期范圍
后臺項目想使用時間選擇器選擇一段時間進行數(shù)據(jù)篩選,所以下面這篇文章主要給大家介紹了關(guān)于Vue2中Element?DatePicker組件設(shè)置默認日期及控制日期范圍的相關(guān)資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2022-11-11
ElementPlus?Table表格實現(xiàn)可編輯單元格
本文主要介紹了ElementPlus?Table表格實現(xiàn)可編輯單元格,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2024-12-12
vue-i18n的9以上版本中@被用作特殊字符處理,直接用會報錯問題
這篇文章主要介紹了vue-i18n的9以上版本中@被用作特殊字符處理,直接用會報錯問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-08-08
vuex(vue狀態(tài)管理)的特殊應(yīng)用案例分享
Vuex 是一個專為 Vue.js 應(yīng)用程序開發(fā)的狀態(tài)管理模式。它采用集中式存儲管理應(yīng)用的所有組件的狀態(tài),并以相應(yīng)的規(guī)則保證狀態(tài)以一種可預(yù)測的方式發(fā)生變化。2020-03-03

