建一个 js 文件
- export
- function addCookie(objName, objValue, objHours) { // 添加 cookie
- var str = objName + "=" + escape(objValue);
- if (objHours > 0) { // 为 0 时不设定过期时间, 浏览器关闭时 cookie 自动消失
- var date = new Date();
- var ms = objHours * 3600 * 1000;
- date.setTime(date.getTime() + ms);
- str += "; expires=" + date.toGMTString();
- }
- document.cookie = str;
- alert("添加 cookie 成功");
- }
- export
- function getCookie(objName) { // 获取指定名称的 cookie 的值
- var arrStr = document.cookie.split(";");
- for (var i = 0; i < arrStr.length; i++) {
- var temp = arrStr[i].split("=");
- if (temp[0] == objName) return unescape(temp[1]);
- }
- }
- export
- function delCookie(name) { // 为了删除指定名称的 cookie, 可以将其过期时间设定为一个过去的时间
- var date = new Date();
- date.setTime(date.getTime() - 10000);
- document.cookie = name + "=a; expires=" + date.toGMTString();
- }
- function allCookie() { // 读取所有保存的 cookie 字符串
- var str = document.cookie;
- if (str == "") {
- str = "没有保存任何 cookie";
- }
- alert(str);
- }
暴露!!!! 一定要
export
在 main.js 写下
- import {addCookie, getCookie, delCookie} from '路径';
- vue.prototype.$cookieStore = {
- addCookie,
- getCookie,
- delCookie
- }
自己路径都填不对的就散了吧
使用
- this.$cookieStore.addCookie( 'name' , 1)
- this.$cookieStore.getCookie( 'name' , 1)
- this.$cookieStore.delCookie( 'name' , 1)
来源: https://www.2cto.com/kf/201802/717581.html