VUE使用cookie

首先执行 :

 

npm install vue-cookies --save

在main.js全局引用

 

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

Api

  • Set a cookie

 

this.$cookies.set(keyName, value[, expireTimes[, path[, domain[, secure]]]])   //return this
  • Get a cookie

 

this.$cookies.get(keyName)       // return value                             
  • Remove a cookie

 

this.$cookies.remove(keyName [, path [, domain]])   // return  false or true , warning: next version return this; use isKey(keyname) return true/false,please
  • Exist a cookie name

 

this.$cookies.isKey(keyName)        // return false or true
  • Get All cookie name

 

this.$cookies.keys()  // return a array

例子

Support chaining sets together

 

 // default expire time: 1 day
this.$cookies.set("user_session","25j_7Sl6xDq2Kc3ym0fmrSSk2xV2XkUkX")    
        // Base of second
        .set("user_session","25j_7Sl6xDq2Kc3ym0fmrSSk2xV2XkUkX",60 * 60 * 24)

// 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] 

Warning

$cookies key names Cannot be set to ['expires','max-age','path','domain','secure']


版权声明:本文为learn8more原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。