Vue之cookie操做(原生)javascript
再vue組件中加入如下幾個方法,而後調用便可。html
methods:{ //讀取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 } }, //設置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()); }, //刪除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(); } },
轉載自:https://www.cnblogs.com/zlfProgrammer/articles/7929973.html前端