(6)添加vue-cookie

#(6)添加vue-cookievue

1 安裝vue-cookie

cnpm install --save vue-cookie

2 引入

main.js中進行引入:git

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

安裝引入後就可使用了github

3 使用方法

The plugin is available through this.$cookie in components or Vue.cookienpm

3.1 例子

// 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 use
this.$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 expires
var 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-cookiecookie

相關文章
相關標籤/搜索