您好,登录后才能下订单哦!
这篇文章主要介绍了如何在vue登录页面中使用cookie,亿速云小编觉得不错,现在分享给大家,也给大家做个参考,一起跟随亿速云小编来看看吧!
Vue是一套用于构建用户界面的渐进式JavaScript框架,Vue与其它大型框架的区别是,使用Vue可以自底向上逐层应用,其核心库只关注视图层,方便与第三方库和项目整合,且使用Vue可以采用单文件组件和Vue生态系统支持的库开发复杂的单页应用。
1、大概流程
a、登录:前端使用validate对输入信息进行验证 验证成功则成功跳转到用户信息页并存储cookie值
b、首页跳转用户信息页:判断cookie值cookie存在并不为空则跳转用户信息页,若为空则跳转登录页
c、退出页:点击退出跳转首页并删除cookie值
2、目录介绍
cookie.js为公共方法,用于cookie的存储、获取及删除
login.vue :登录页
index.vue:首页
user.vue:用户信息页
myinfo.vue:退出页
3、文件内容
a、cookie.js
/*用export把方法暴露出来*/
/*设置cookie*/
export function setCookie(c_name,value,expire) {
var date=new Date()
date.setSeconds(date.getSeconds()+expire)
document.cookie=c_name+ "="+escape(value)+"; expires="+date.toGMTString()
//console.log(document.cookie)
}
/*获取cookie*/
export function getCookie(c_name){
if (document.cookie.length>0){
let c_start=document.cookie.indexOf(c_name + "=")
if (c_start!=-1){
c_start=c_start + c_name.length+1
let c_end=document.cookie.indexOf(";",c_start)
if (c_end==-1) c_end=document.cookie.length
return unescape(document.cookie.substring(c_start,c_end))
}
}
return ""
}
/*删除cookie*/
export function delCookie(c_name){
setCookie(c_name, "", -1)
}b、login.vue
methods:{
submit(){
setCookie('username',username,1000*60)
axios.get('http://172.16.2.43:8080/static/data/mt_index.json').then((res)=>{
this.$router.push({
path: '/user', query:{userid: $("input[name='username']").val()}
});
//this.setuserid($("input[name='username']").val());
})
}
}c、index.vue
<div class="topheader">
<span class="location fl">北京</span>
<div class="search-box">
<a href=""><input type="text"></a>
</div>
<span class="mine" @click="jampmin">我的</span>
</div>
jampmin(){
//获取cookie值
var username=getCookie('username');
if(username==''||username=='undefind'){
this.$router.push({
path: '/login'
});
}else{
this.$router.push({
path: '/user'
});
}
}d、myinfo.vue
<p @click="signout()" class="signout">退出</p>
signout(){
/*删除cookie*/
delCookie('username');
this.$router.push({
path: '/index'
});
}以上就是亿速云小编为大家收集整理的如何在vue登录页面中使用cookie,如何觉得亿速云网站的内容还不错,欢迎将亿速云网站推荐给身边好友。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。