其他学习

VUE使用cookie

2018-11-13  本文已影响43人  weisen

文章参考:https://blog.csdn.net/qq_35573326/article/details/82049654

首先执行 :

npm install vue-cookies --save

在main.js全局引用

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

Api

this.$cookies.set(keyName, value[, expireTimes[, path[, domain[, secure]]]])   //return this
this.$cookies.get(keyName)       // return value                             
this.$cookies.remove(keyName [, path [, domain]])   // return  false or true , warning: next version return this; use isKey(keyname) return true/false,please
this.$cookies.isKey(keyName)        // return false or true
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']

上一篇 下一篇

猜你喜欢

热点阅读