添加vue-cookie

2018-11-26  本文已影响0人  君临12138

1 安装vue-cookie

cnpm install --save vue-cookie

2 引入

在main.js中进行引入:

import Vue from 'vue' //这句是原来就有的
import VueCookie from 'vue-cookie'
Vue.use(VueCookie)

安装引入后就可以使用了

3 使用方法

// From some method in one of your Vue components
this.$cookie.set('test', 'Hello world!', 1);
// This will set a cookie with the name 'test' and the value 'Hello world!' that expires in one day// To get the value of a cookie use
this.$cookie.get('test');// To delete a cookie usethis.$cookie.delete('test');

3.2 高级例子

// Setting the cookie Domain
this.$cookie.set('test', 'Random value', {expires: 1, domain: 'localhost'});
// As this cookie is set with a domain then if you wish to delete it you have to provide the domain when calling delete
this.$cookie.delete('test', {domain: 'localhost'});
// Customizing expiresvar date = new Date;
date.setDate(date.getDate() + 21);
this.$cookie.set('dateObject', 'A date object', { expires: date });
this.$cookie.set('dateString', 'A parsable date string', { expires: date.toGMTString() })
;this.$cookie.set('integer', 'Seven days later', { expires: 7 });
this.$cookie.set('stringSuffixY', 'One year later', { expires: '1Y' });
this.$cookie.set('stringSuffixM', 'One month later', { expires: '1M' });
this.$cookie.set('stringSuffixD', 'One day later', { expires: '1D' });
this.$cookie.set('stringSuffixh', 'One hour later', { expires: '1h' });
this.$cookie.set('stringSuffixm', 'Ten minutes later', { expires: '10m' });
this.$cookie.set('stringSuffixs', 'Thirty seconds later', { expires: '30s' });

3.3 网上找到的语法

this.$cookies.set(keyName, value[, expireTimes[, path[, domain[, secure]]]]) //return this
this.$cookies.get(keyName) // return value
this.$cookies.remove(keyName [, path [, domain]]) // return false or true , warning: next version return this; 
use isKey(keyname) return true/false,please

参考

https://github.com/alfhen/vue-cookie

上一篇下一篇

猜你喜欢

热点阅读