vue-cookies

vue-cookies

一个简单的Vue.js插件,用于处理浏览器cookie

安装

npm install vue-cookies --save

使用

import Vue from 'Vue'
import VueCookies from 'vue-cookies'
Vue.use(VueCookies)

Api

语法格式: [this | Vue | window].$cookies.[method]

  • 设置全局配置
$cookies.config(expireTimes[,path])  // default: expireTimes = 1d , path=/
  • 设置cookie
$cookies.set(keyName, value[, expireTimes[, path[, domain[, secure]]]])   //return this
  • 获取cookie
$cookies.get(keyName)  // return value                             
  • 删除cookie
$cookies.remove(keyName [, path [, domain]])  // return this
  • 查看cookie是否存在(通过KeyName)
$cookies.isKey(keyName)  // return false or true
  • 获取所有cookie名
this.$cookies.keys()  // return a array

示例用法

设置全局配置

// 30 day after, expire
this.$cookies.config('30d')

this.$cookies.config(new Date(2019,03,13).toUTCString())

// 30 day after, expire, '' current path , browser default
this.$cookies.config(60 * 60 * 24 * 30,'');

// window object
window.$cookies.config('30d')

支持 json 对象

var user = { id:1, name:'Journal',session:'25j_7Sl6xDq2Kc3ym0fmrSSk2xV2XkUkX' };
this.$cookies.set('user',user);
// print user name
console.log(this.$cookies.get('user').name)

设置过期时间

假设当前时间是:2017年3月11日星期六12:25:57 GMT

等效:1天后,到期

支持链接集合在一起       

 //默认过期时间:1天

this.$cookies.set("user_session","25j_7Sl6xDq2Kc3ym0fmrSSk2xV2XkUkX")

        // number + d , 忽略大小写

        .set("user_session","25j_7Sl6xDq2Kc3ym0fmrSSk2xV2XkUkX","1d")

        .set("user_session","25j_7Sl6xDq2Kc3ym0fmrSSk2xV2XkUkX","1D")

        // Base of second

        .set("user_session","25j_7Sl6xDq2Kc3ym0fmrSSk2xV2XkUkX",60 * 60 * 24)

        // 输入日期, + 1day

        .set("user_session","25j_7Sl6xDq2Kc3ym0fmrSSk2xV2XkUkX", new Date(2017, 03, 12))

        // 输入日期字符串, + 1day

        .set("user_session","25j_7Sl6xDq2Kc3ym0fmrSSk2xV2XkUkX", "Sat, 13 Mar 2017 12:25:57 GMT")

设置过期时间,输入数字类型。

this.$cookies.set("default_unit_second","input_value",1);            // 1 second after, expire
this.$cookies.set("default_unit_second","input_value",60 + 30);      // 1 minute 30 second after, expire
this.$cookies.set("default_unit_second","input_value",60 * 60 * 12); // 12 hour after, expire
this.$cookies.set("default_unit_second","input_value",60 * 60 * 24 * 30); // 1 month after, expire

设置过期时间 - 浏览器会话结束。

this.$cookies.set("default_unit_second","input_value","0");          // end of session - use string!

设置过期时间 , 输入字符串类型。

Unit full name
y year
m month
d day
h hour
min minute
s second

单位名称忽略大小写

不支持组合

不支持双值

this.$cookies.set("token","GH1.1.1689020474.1484362313","60s");  // 60秒之后, 过期

this.$cookies.set("token","GH1.1.1689020474.1484362313","30MIN");  // 30 分钟后, 过期, 忽略大小写

this.$cookies.set("token","GH1.1.1689020474.1484362313","24d");  // 24天后r, 过期

this.$cookies.set("token","GH1.1.1689020474.1484362313","4m");  // 4 个月后, 过期

this.$cookies.set("token","GH1.1.1689020474.1484362313","16h");  // 16小时后, 过期

this.$cookies.set("token","GH1.1.1689020474.1484362313","3y");  // 3 年后, 过期 
// 输入日期字符串
this.$cookies.set('token',"GH1.1.1689020474.1484362313", new Date(2017,03,13).toUTCString());

this.$cookies.set("token","GH1.1.1689020474.1484362313", "Sat, 13 Mar 2017 12:25:57 GMT ");

设置过期支持日期

var date = new Date;
date.setDate(date.getDate() + 1);
this.$cookies.set("token","GH1.1.1689020474.1484362313", date);

设置永不过期

this.$cookies.set("token","GH1.1.1689020474.1484362313", Infinity);  // never expire
// never expire , only -1,Other negative Numbers are invalid
this.$cookies.set("token","GH1.1.1689020474.1484362313", -1); 

设置其他参数

// set path
this.$cookies.set("use_path_argument","value","1d","/app");  

// set domain
this.$cookies.set("use_path_argument","value",null, null, "domain.com");   // default 1 day after,expire

// set secure
this.$cookies.set("use_path_argument","value",null, null, null,true);

其他操作

// check a cookie exist
this.$cookies.isKey("token")

// get a cookie
this.$cookies.get("token");

// remove a cookie
this.$cookies.remove("token");

// get all cookie key names, line shows
this.$cookies.keys().join("\n"); 

// vue-cookies global
[this | Vue | window].$cookies.[method] 

警告

$cookies 秘钥名称不能设置为 ['expires','max-age','path','domain','secure']

说明

vue-cookies没有依赖关系,它可以独立存在,对vuejs友好

 

你可能感兴趣的:(Vue,前端,VueCookies)