Vue-CLI project -vue-cookie cookie treated with vue-cookies

08.31 self-summary

Vue-CLI project -vue-cookie cookie treated with vue-cookies

A. Mounting module

npm install vue-cookie --save
#--save可以不用写

II. Configuration main.js

// 配置cookie
import cookie from 'vue-cookie'
Vue.prototype.$cookie = cookie;  //配置时候prototype.$这里的名字自己定义不是固定是cookie

III. Use

created() {//创建时间节点
    console.log('组件创建成功');
    let token = 'asd1d5.0o9utrf7.12jjkht';
    // 设置cookie默认过期时间单位是1d(1天)
    this.$cookie.set('token', token, 1);
},
mounted() {//创建渲染节点
    console.log('组件渲染成功');
    let token = this.$cookie.get('token');
    console.log(token);
},
destroyed() {//组件销毁节点
    console.log('组件销毁成功');
    this.$cookie.delete('token')
}

Overview:

  • 创建:. This $ configuration when disposed name .set ( 'cookies of key', value, the minimum time in days and must be an integer)
  • 获取指定的key: This $ configuration name when setting .get ( 'cookies are key`).
  • 删除: this $ configuration name when setting.. delete( 'Cookies of key`)
    • If you do not delete this restart your browser cookies also, but is empty
    • Restart the browser cookies before disappearing

view-cookies

Install a Module

npm install vue-cookies --save
#--save可以不用写

II. Configuration main.js

// 配置cookie
import cookies from 'vue-cookies'
Vue.prototype.$cookies = cookies;  //配置时候prototype.$这里的名字自己定义不是固定是cookies

III. Use

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
    • 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/pythonywy/p/11440910.html