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 can I implement pinch zoom functionality in a Vue.js project?
- How can I convert XML data to JSON using Vue.js?
- How can I use the Vue.js UI library to develop a web application?
- How can I use Vue and Chart.js to add zoom functionality to my chart?
- How do I host a website using Vue.js?
- How do I download a zip file using Vue.js?
- How do I set a z-index in Vue.js?
- How do I use Yup with Vue.js?
- How do I integrate Yandex Maps with Vue.js?
- How to use a YAML editor with Vue.js?
See more codes...