Vue - process cookie with Vue-cookie with Vue-cookies

Vue-cookie cookie handling

Under the front end of the terminal project directory: Installation

npm install vue-cookie --save

Configuration: main.js

import cookie from 'vue-cookie'
Vue.prototype.$cookie = cookie;

Use: logical methods assembly

created(){
    console.log('组件创建成功');
    let token = 'asdsdfs.sdfsdf.sdfaefw';
    this.$cookie.set('token',token)  // 设置cookie,默认过期时间单位是1d(1天)
    // this.$cookie.set('token',token,10) 过期时间是10天
},
mounted(){
    console.log('组件渲染成功');
    let token = this.$cookie.get('token');  // 获取cookie
    console.log(token);
},
destroyed(){
    console.log('组件销毁成功');
    this.$cookie.delete('token')  // 删除cookie
}

When you delete a cookie, during the execution of two: the empty cookie value, the expiration time set forth

Vue-cookies cookie handling

Under the front end of the terminal project directory: Installation

npm install vue-cookies --save

Configuration: main.js

import cookies from 'vue-cookies'
Vue.prototype.$cookies = cookies;

Use: logical methods assembly

created(){
    console.log('组件创建成功');
    let token = 'asdsdfs.sdfsdf.sdfaefw';
    this.$cookies.set('token',token)  // 设置cookie,默认过期时间单位是1d(1天)
    // this.$cookies.set('token',token,10) 过期时间是10天
},
mounted(){
    console.log('组件渲染成功');
    let token = this.$cookies.get('token');  // 获取cookie
    console.log(token);
},
destroyed(){
    console.log('组件销毁成功');
    this.$cookies.remove('token')  // 删除cookie
}

Overview:

  • 创建:. This $ configuration name when setting .set ( 'cookies of key', value)
  • 获取指定的key: This $ configuration name when setting .get ( 'cookies are key`).
  • 获取所有keys返回为数组的形式:. This $ configuration settings when the name .keys ( 'cookies are key`)
  • 删除: this $ configuration name when setting.. remove( 'Cookies of key`)
  • If you do not delete this restart your browser cookies also, but is empty
    • When you delete a cookie, during the execution of two: the empty cookie value, the expiration time set forth
    • Restart the browser cookies before disappearing
  • 检查某个 cookie name是否存在:. This $ configuration settings when the name .isKey ( 'cookies are key`)

Related:

  • Expiration Global Settings

这里是全局的设置所有的cookie都会生效

this.$cookies.config('固定时间') //填的值1d为一天,1h为一小时,1min为一分钟,1s为1秒

//指定时间
this.$cookies.config(new Date(2020,12,1))
this.$cookies.config("Sat, 13 Mar 2017 12:25:57 GMT")

//如果是乘法
this.$cookies.config(60*60) //是60s*60s依次类推

//如果是单单空数组
this.$cookies.config(60) //也是60S
  • Single cookie settings
//不写过期时间,默认为1天过期
this.$cookies.set("user_session","25j_7Sl6xDq2Kc3ym0fmrSSk2xV2XkUkX")

// 1天过期,忽略大小写
this.$cookies.set("user_session","25j_7Sl6xDq2Kc3ym0fmrSSk2xV2XkUkX","1d")
this.$cookies.set("user_session","25j_7Sl6xDq2Kc3ym0fmrSSk2xV2XkUkX","1D")

// 以秒为单位,设置1天过去
this.$cookies.set("user_session","25j_7Sl6xDq2Kc3ym0fmrSSk2xV2XkUkX",60 * 60 * 24)

// 填写Date对象,明确指定过期时间
this.$cookies.set("user_session","25j_7Sl6xDq2Kc3ym0fmrSSk2xV2XkUkX", new Date(2017, 03, 12))

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

//浏览器会话结束时过期
this.$cookies.set("default_unit_second","input_value","0");
 
//永不过期
this.$cookies.set("default_unit_second","input_value",-1); 

Set the expiration time, the input string types (characters are ignored):

Unit full name
Y year
m month
d day
h hour
me minute
s second

Guess you like

Origin www.cnblogs.com/863652104kai/p/11443979.html