創(chuàng)新互聯(lián)公司總部坐落于成都市區(qū),致力網(wǎng)站建設(shè)服務(wù)有做網(wǎng)站、成都網(wǎng)站設(shè)計(jì)、網(wǎng)絡(luò)營(yíng)銷策劃、網(wǎng)頁(yè)設(shè)計(jì)、網(wǎng)站維護(hù)、公眾號(hào)搭建、微信小程序定制開發(fā)、軟件開發(fā)等為企業(yè)提供一整套的信息化建設(shè)解決方案。創(chuàng)造真正意義上的網(wǎng)站建設(shè),為互聯(lián)網(wǎng)品牌在互動(dòng)行銷領(lǐng)域創(chuàng)造價(jià)值而不懈努力!
實(shí)現(xiàn)功能:
1.一周內(nèi)自動(dòng)登錄勾選時(shí),將賬號(hào)和密碼保存到cookie,下次登陸自動(dòng)顯示到表單內(nèi)
2.點(diǎn)擊忘記密碼則清空之前保存到cookie的值,下次登陸需要手動(dòng)輸入
次要的就不說(shuō)了直接上主要的代碼
html部分
<el-form :model="ruleForm" :rules="rules" ref="ruleForm" class="demo-ruleForm loginFrom"> <h2 >登陸</h2> <el-form-item > <el-input placeholder="賬號(hào)" 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 class="clear"> <span class="lf" >幫助</span> <div class="rt"> <el-checkbox v-model="checked" >一周內(nèi)自動(dòng)登錄</el-checkbox> <span @click="clearCookie" >忘記密碼?</span> </div> </div> <el-button type="primary" @click="submitForm('ruleForm')" >登陸</el-button> </el-form>
js部分
data () { return { ruleForm: { userName: '', //用戶名 password: '' //密碼 }, } } methods: { //點(diǎn)擊登錄調(diào)用方法 submitForm(formName) { //保存的賬號(hào) var name=this.ruleForm.userName; //保存的密碼 var pass=this.ruleForm.password; if(name==''||name==null){ alert("請(qǐng)輸入正確的用戶名"); return }else if(pass==''||pass==null) { alert("請(qǐng)輸入正確的密碼"); return } //判斷復(fù)選框是否被勾選 勾選則調(diào)用配置cookie方法 if(this.checked=true){ //傳入賬號(hào)名,密碼,和保存天數(shù)3個(gè)參數(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("用戶名或密碼錯(cuò)誤,請(qǐng)重新輸入"); 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();//獲取時(shí)間 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('=');//再次切割 //判斷查找相對(duì)應(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ù)為負(fù)1天就好了 } }, //頁(yè)面加載調(diào)用獲取cookie值 mounted(){ this.getCookie() }
總結(jié)
以上所述是小編給大家介紹的vue實(shí)現(xiàn)表單登錄頁(yè)保存賬號(hào)和密碼到cookie功能,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)創(chuàng)新互聯(lián)網(wǎng)站的支持!
本文題目:vue項(xiàng)目實(shí)現(xiàn)表單登錄頁(yè)保存賬號(hào)和密碼到cookie功能
URL分享:http://chinadenli.net/article34/ihjcpe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供ChatGPT、微信公眾號(hào)、手機(jī)網(wǎng)站建設(shè)、關(guān)鍵詞優(yōu)化、網(wǎng)站建設(shè)、動(dòng)態(tài)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)