原本是打算登陆的时候把用户名传过去,试了几回都没成功,而后改为用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
这样就能够作一个登陆和退出的操做了模板