vue保存及应用cookieStore
原创vue保存和使用cookieStore
1.定义
utils/common.js
//获取cookie、
export function getCookie(name) {
var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
if (arr = document.cookie.match(reg))
return (arr[2]);
else
return null;
}
//设置cookie,增加到vue实例便于全局调用。
export function setCookie(c_name, value, expiredays) {
var exdate = new Date();
exdate.setDate(exdate.getDate() + expiredays);
document.cookie = c_name + "=" + escape(value) + ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString());
};
//删除cookie
export function 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();
};
export default {
getCookie,
setCookie,
delCookie,
}
2.全局
main.js
import cookieStore from @/utils/common;
Vue.prototype.$cookieStore = cookieStore;
3。获取
在App.vue
created() {
//初始化全局拦截 集成框架url
if (window.location.hash.split("data")[1] !== undefined) {
let url = window.location.hash.split("data")[1].split("=")[1];//获取截取部分
let common = JSON.parse(unescape(url));//解码截获的部分
console.log("dwafcommon",common);
//存
this.$cookieStore.setCookie("common", JSON.stringify(common));
//取
//JSON.parse(unescape(this.$cookieStore.getCookie("common")))
//存
sessionStorage.setItem("pageInfo", JSON.stringify(common));
//取
//JSON.parse(sessionStorage.getItem(pageInfo))
} else {}
- JSON.parse()用法
JSON.parse()方法将JSON格式字符串已转换。js对象(属性名称没有双引号。)。
在分析之前,请确保数据是标准的。JSON格式,否则将解决错误。 - JSON.stringify() 方法将是 JavaScript 对象或值已转换 JSON 字符串
4.使用
import cookieStore from "@/utils/common";
JSON.parse(unescape(cookieStore.getCookie("userIdCom")))//userIdCom
番外:
this.urlhref="yourUrlHref"
let number = 0;
for (let key in FORM) {
if (FORM[key] != null && FORM[key] != && FORM[key].length != 0) {
if (key != "comName") {
if (number == 0) {
this.urlhref += "?" + key + "=" + FORM[key];
} else {
this.urlhref += "&" + key + "=" + FORM[key];
}
number++;
}
}
}
版权声明
所有资源都来源于爬虫采集,如有侵权请联系我们,我们将立即删除