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 to use a YAML editor with Vue.js?
- How do I change the z-index of a modal in Vue.js?
- How do I set a z-index in Vue.js?
- How can I use Vue and Chart.js to add zoom functionality to my chart?
- How do I determine which version of Vue.js I am using?
- How do I use Yup with Vue.js?
- How do I set up unit testing for a Vue.js application?
- How do I use Vue.js lifecycle hooks?
- How can I use Vue.js with Laravel to create an effective web application?
- How do I make an XHR request with Vue.js?
See more codes...