#(6)添加vue-cookievue
cnpm install --save vue-cookie
在main.js
中進行引入:git
import Vue from 'vue' //這句是原來就有的 import VueCookie from 'vue-cookie' Vue.use(VueCookie)
安裝引入後就可使用了github
The plugin is available through
this.$cookie
in components orVue.cookie
npm
// 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');
// 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' });
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