vue-cookies-ts
A simple Vue.js plugin for handling browser cookies
Installation
npm install vue-cookies-ts --save
Vue.useVueCookies
Api
syntax format: [this | Vue | window].$cookies.[method]
config
Set global config
void //example this.$cookies.config // default: expireTimes = 1d , path=/
set
Set a cookie
VueCookies //example this.$cookies.setkeyName: string,
get
Get a cookie
string | null | object //example this.$cookies.getkeyName: string
remove
Remove a cookie
VueCookies | boolean //example this.$cookies.removekeyName: string,
isKey
If exist a cookie name
boolean //example this.$cookies.isKeykeyName: string
keys
Get All cookie name
string //example this.$cookies.keys
Example Usage
Set global config
// 30 day after, expirethis$cookies this$cookies // 30 day after, expire, '' current path , browser defaultthis$cookies // window objectwindow$cookies
Support json object
var user = id:1 name:'Journal' session:'25j_7Sl6xDq2Kc3ym0fmrSSk2xV2XkUkX' this$cookies // print user nameconsole
Set expire times
Suppose the current time is : Sat, 11 Mar 2017 12:25:57 GMT
Following equivalence: 1 day after, expire
Support chaining sets together
// default expire time: 1 daythis$cookies// number + d , ignore case// Base of second// input a Date, + 1day// input a date string, + 1day
Set expire times, input number type
// 1 second after, expirethis$cookies // 1 minute 30 second after, expirethis$cookies // 12 hour after, expirethis$cookies // 1 month after, expirethis$cookies
Set expire times - end of browser session
// end of session - use string!this$cookies
Set expire times , input string type
Unit | full name |
---|---|
y | year |
m | month |
d | day |
h | hour |
min | minute |
s | second |
✔ caseless for unit
❌ combination not supported
❌ double value not supported
// 60 second after, expirethis$cookies // 30 minute after, expire, ignore casethis$cookies // 24 day after, expirethis$cookies // 4 month after, expirethis$cookies // 16 hour after, expirethis$cookies // 3 year after, expirethis$cookies // input date string this$cookies this$cookies
Set expire support date
var date = date this$cookies
Set never expire
// never expirethis$cookies // never expire , only -1,Other negative Numbers are invalidthis$cookies
Set other arguments
// set paththis$cookies // set domain, default 1 day after,expirethis$cookies // set securethis$cookies
Other operation
// check a cookie existthis$cookies // get a cookiethis$cookies // remove a cookiethis$cookies // get all cookie key names, line showsthis$cookies // vue-cookies globalthis | Vue$cookiesmethod
Warning
$cookies
key names Cannot be set to ['expires','max-age','path','domain','secure']
Explaination
vue-cookies-ts is developed from vue-cookies, It can exist independently, Friendly to vuejs
License
MIT Copyright (c) 2016-present, ztytotoro