Vue如何使用Cookie?

1.下载依赖(官方文档:https://www.npmjs.com/package/vue-cookies)

1
npm install -g vue-cookies

2.在main.js引入依赖

1
2
3
4
5
6
7
import Vue from 'vue'
import App from './App'
import router from './router'
import VueCookies from 'vue-cookies'


Vue.use(VueCookies)

3.操作Cookie

3.1写Cookie

1
this.$cookies.set(keyName, value[, expireTimes[, path[, domain[, secure[, sameSite]]]]])   //return this

3.2读Cookie

1
this.$cookies.get(keyName)  // return value

3.3移除Cookie

1
this.$cookies.remove(keyName [, path [, domain]])  // return this

3.4判断一个Cookie是否存在

1
this.$cookies.isKey(keyName)  // return false or true

3.5获取所有Cookie的key

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