Vue 獲取登陸用戶名

原本是打算登陸的時候把用戶名傳過去,試了幾回都沒成功,而後改爲用cookie保存用戶名,而後在讀取就好了,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();
}get

獲取cookoestring

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;
}
}
}it

設置用戶名的時候得寫在鉤子函數裏面,否則模板不會被渲染。io

退出的時候能夠刪除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();
}function

這樣就能夠作一個登陸和退出的操做了模板

相關文章
相關標籤/搜索