vue.jsHow do I store and retrieve cookies in Vue.js?
Storing and retrieving cookies in Vue.js is easy to do with the vue-cookies package.
First, install the package with npm install vue-cookies --save.
Then, import the package in the main.js file of your Vue.js project:
import VueCookies from 'vue-cookies'
Vue.use(VueCookies)
Once imported, you can set and retrieve cookies like this:
// Set a cookie
this.$cookies.set('name', 'John Doe')
// Retrieve a cookie
let name = this.$cookies.get('name')
// Output: John Doe
console.log(name)
You can also set cookie options such as expiration date and domain:
this.$cookies.set('name', 'John Doe', '2h')
The above will set the cookie to expire in 2 hours.
You can also delete a cookie like this:
this.$cookies.remove('name')
This will delete the cookie with the key name.
More of Vue.js
- How do I change the z-index of a modal in Vue.js?
- How can I use Vue and Chart.js to add zoom functionality to my chart?
- How do I download a zip file using Vue.js?
- How do I set a z-index in Vue.js?
- How can I use Vue.js to address a Common Vulnerabilities and Exposures (CVE) issue?
- How can I use Vue.js to implement a zoomable image?
- How do I integrate Yandex Maps with Vue.js?
- How to use a YAML editor with Vue.js?
- How do I make an XHR request with Vue.js?
- How can I convert XML data to JSON using Vue.js?
See more codes...