9951 explained code solutions for 126 technologies


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.

Edit this code on GitHub