Vue 獲取登錄用戶名


本來是打算登錄的時候把用戶名傳過去,試了幾次都沒成功,然后改成用cookie保存用戶名,然后在讀取就行了,

登錄時候設置cookie

setCookie(c_name,c_pwd,exdays) {
var exdate=new Date();
exdate.setTime(exdate.getTime() + 24*60*60*1000*exdays);
window.document.cookie="userName"+ "=" +c_name+";path=/;expires="+exdate.toGMTString();
window.document.cookie="userPwd"+"="+c_pwd+";path=/;expires="+exdate.toGMTString();
}

獲取cookoe

getCookie:function () {
if (document.cookie.length>0) {
var arr=document.cookie.split('; ');
if(arr[1].indexOf("userPwd")!=-1){
let arr2=arr[1].substring(arr[1].indexOf("=")+1);
return arr2;
}
}
}

設置用戶名的時候得寫在鈎子函數里面,不然模板不會被渲染。

退出的時候可以刪除cookie

delCookie:function(name){
var exp = new Date();
exp.setTime(exp.getTime() - 1);
var cval=getCookie(name);
if(cval!=null)
document.cookie= name + "="+cval+";expires="+exp.toGMTString();
}

這樣就可以做一個登錄和退出的操作了


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM