項目須要前端獲取後臺返回的cookie,並以此做判斷。我是在main.js入口文件下使用的前端
具體代碼:後端
new Vue({
el: '#app',
router,
template: '<App/>',
components: { App },
methods:{cookie
//讀取cookie,須要注意的是cookie是不能存中文的,若是須要存中文,解決方法是後端先進行編碼encode(),前端取出來以後用decodeURI('string')解碼。(安卓能夠取中文cookie,IOS不行)
getCookie(name) {
var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
if (arr = document.cookie.match(reg)){
return true;
// return (arr[2]);
}else{
return false
}
},app
//設置cookie name爲cookie的名字,value是值,expiredays爲過時時間(天數)
setCookie (name, value, expiredays) {
var exdate = new Date();
exdate.setDate(exdate.getDate() + expiredays);
document.cookie = c_name + "=" + escape(value) + ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString());
},this
//刪除cookie編碼
delCookie (name) {
var exp = new Date();
exp.setTime(exp.getTime() - 1);
var cval = getCookie(name);
if (cval != null)
document.cookie = name + "=" + cval + ";expires=" + exp.toGMTString();
}code
},
created(){
this.setCookie('openId',123,2)
if (this.getCookie('openId')) {
console.log('has cookie')component
this.delCookie ('openId')
}else{
console.log('has not cookie')
}
}
})router
單純貼碼,拋磚引玉!get