Use the vue-cookies plugin to operate cookies and set the token validity period

1. Use the plugin vue-cookies to install

npm install vue-cookies --save

2. Global import, set in main.js in the project file

// vue-cookies配置
import cookies from 'vue-cookies';

// vue2中的设置方式
Vue.prototype.$cookies = cookies;

// vue3中的设置方式
app.config.globalProperties.$cookies=cookies;

3. Example of use

time unit:

//不写过期时间,默认为1天过期
this.$cookies.set("token","123456")

// 1天过期,忽略大小写
this.$cookies.set("token","12346","1d")
this.$cookies.set("token","123456","1D")
// 以秒为单位,设置1天过去
this.$cookies.set("token","123456",60 * 60 * 24)

// 填写Date对象,明确指定过期时间
this.$cookies.set("token","123456", new Date(2020, 10, 01))

// 填写一个时间字符串,指定过期时间
this.$cookies.set("token","123456", "Sat, 13 Mar 2017 12:25:57 GMT")

//浏览器会话结束时过期
this.$cookies.set("token","input_value","0");

//永不过期
this.$cookies.set("token","input_value",-1); 

4. Common examples

 1. Set cookies

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

// 简洁使用
this.$cookies.set('key','value', '过期时间,按秒计');

// 示例
this.$cookies.set('username',response.data.username, '7d');

2. Get cookies

this.$cookies.get('key')       // return value

3. Delete cookies

this.$cookies.remove("key")

4. Determine whether there is a specified key in the cookies

this.$cookies.isKey('key')

5. Get all cookies

this.$cookies.keys()

Guess you like

Origin blog.csdn.net/m0_69257679/article/details/131780702