vue-cookie的使用

下载模块:npm install vue-cookie --save

//引入
import Vue from 'vue'
import VueCookie from 'vue-cookie'
Vue.use(VueCookie);
//设置cookie
//this指向vue对象
//写法一 :第一个参数key值   第二个参数value值   第三个参数:期限值,到期就会清除
this.$cookie.set('test' , 'Hello world!', 1); 
//写法二:
//获取时间对象
var date = new Date;
date.setDate(date.getDate() + 21);

this.$cookie.set('dateObject', '一个日期对象的时间戳', { expires: date });
this.$cookie.set('dateString', '一个日期字符串Tue, 12 Apr 2022 09:10:29 GMT', { expires: date.toGMTString() });//用toGMTString对时间戳转字符串
this.$cookie.set('integer', '期限为七天', { expires: 7 });
this.$cookie.set('stringSuffixY', '期限为一年', { expires: '1Y' });
this.$cookie.set('stringSuffixM', '期限为一个月', { expires: '1M' });
this.$cookie.set('stringSuffixD', '期限为一天', { expires: '1D' });
this.$cookie.set('stringSuffixh', '期限为一小时', { expires: '1h' });
this.$cookie.set('stringSuffixm', '期限为10分钟', { expires: '10m' });
this.$cookie.set('stringSuffixs', '期限为30秒后', { expires: '30s' });

//如果设置了域,就对加上域
this.$cookie.set('test', 'Random value', {expires: 1, domain: 'localhost'}); //domain:域地址 https ://www.baidu.com
//删除cookie字段
this.$cookie.delete('test', {domain: 'localhost'});

参考网址:https://www.npmjs.com/package/vue-cookie#advanced-examples

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值