vue-cookie2
A Vue.js plugin for manipulating cookies tested up to Vue v2.5.17
Installation
Install through npm or yarn:
# Use NPMnpm install vue-cookie2 --save# Or use yarnyarn add vue-cookie2
Include in <body>
after loading Vue and it will automatically hook into Vue:
Or do it the cool way and load it in your main.js/app.js
:
// Require dependenciesvar Vue = ;var VueCookie = ;// Tell Vue to use the pluginVue;
Usage
The plugin is available through this.$cookie
in components or Vue.cookie
.
Rather than implementing my own Cookie handling logic the plugin now wraps the awesome
tiny-cookie package.
Example
// From some method in one of your Vue componentsthis$cookie;// 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 usethis$cookie;// To get all cookiesthis$cookie;// To delete a cookie usethis$cookie;// Flush all cookiesthis$cookie;
Advanced examples
There are some options can be set in the third argument.
Domain
// Setting the cookie Domainthis$cookie; // As this cookie is set with a domain then if you wish to delete it you have to provide the domain when calling deletethis$cookie;
Expires
You can set the cookie expire time as you want.
var date = ;date; this$cookie;this$cookie;this$cookie;this$cookie;this$cookie;this$cookie;this$cookie;this$cookie;this$cookie;
Raw Data
We'll using encodeURIComponent/decodeURIComponent on set/get.
If you want to set/get raw data, you can use setRaw/getRaw method.
this$cookie;this$cookie;
Thanks for using the plugin, I am happy to accept feedback/pull requests, do not forget to star if you like it!
Happy Coding! :D
Tests
This packacge uses the ´´´testemframework and
jasmine``` assertion library
# Run npm install to fetch dependenciesyarn install# Then you may run the tests fromyarn run test:dev
Acknowledgement
This plugin is based on alfhen/vue-cookie. Thank you for making the perfect plugin.