Vue-cookies Perform data caching on the front end. If remember password is selected each time, the account information is stored locally. If remember password is not selected, the local account information is cleared.
Browser
<script src="https://unpkg.com/vue/dist/vue.js"></script> <script SRC = "https://unpkg.com/[email protected]/vue-cookies.js" > < / script >Copy the code
Package Managers
npm install vue-cookies --save
// require
var Vue = require('vue')
Vue.use(require('vue-cookies'))
// es2015 module
import Vue from 'vue'
import VueCookies from 'vue-cookies'
Vue.use(VueCookies)
// set default config
Vue.$cookies.config('7d')
// set global cookie
Vue.$cookies.set('theme','default');
Vue.$cookies.set('hover-time','1s');
Copy the code
Example Usage
set global config
// 30 day after, expire
Vue.$cookies.config('30d')
// set secure, only https works
Vue.$cookies.config('7d','','',true)
// 2019-03-13 expire
this.$cookies.config(new Date(2019,03,13).toUTCString())
// 30 day after, expire, '' current path , browser default
this.$cookies.config(60 * 60 * 24 * 30,'');
Copy the code
support json object
var user = { id:1, name:'Journal',session:'25j_7Sl6xDq2Kc3ym0fmrSSk2xV2XkUkX' };
this.$cookies.set('user',user);
// print user name
console.log(this.$cookies.get('user').name)
Copy the code
set expire times
this.$cookies.set("user_session","25j_7Sl6xDq2Kc3ym0fmrSSk2xV2XkUkX")
// number + d , ignore case
.set("user_session","25j_7Sl6xDq2Kc3ym0fmrSSk2xV2XkUkX","1d")
.set("user_session","25j_7Sl6xDq2Kc3ym0fmrSSk2xV2XkUkX","1D")
// Base of second
.set("user_session","25j_7Sl6xDq2Kc3ym0fmrSSk2xV2XkUkX",60 * 60 * 24)
// input a Date, + 1day
.set("user_session","25j_7Sl6xDq2Kc3ym0fmrSSk2xV2XkUkX", new Date(2017, 03, 12))
// input a date string, + 1day
.set("user_session","25j_7Sl6xDq2Kc3ym0fmrSSk2xV2XkUkX", "Sat, 13 Mar 2017 12:25:57 GMT")
Copy the code
set expire times – end of browser session
this.$cookies.set("default_unit_second","input_value",0); // end of session - use 0 or "0"!
Copy the code
set never expire
This $cookies. Set (" token ", "GH1.1.1689020474.1484362313", Infinity); // never expire // never expire , Only 1, Other negative Numbers are invalid enclosing $cookies, set (" token ", "GH1.1.1689020474.1484362313", 1);Copy the code
remove cookie
this.$cookies.set("token",value); // domain.com and *.doamin.com are readable
this.$cookies.remove("token"); // remove token of domain.com and *.doamin.com
this.$cookies.set("token", value, null, null, "domain.com"); // only domain.com are readable
this.$cookies.remove("token", null, "domain.com"); // remove token of domain.com
Copy the code
other operation
// 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");
// remove all cookie
this.$cookies.keys().forEach(cookie => this.$cookies.remove(cookie))
// vue-cookies global
[this | Vue].$cookies.[method]
Copy the code