The Cookie Module for Nuxt, works perfectly on the client side and on the server side for set, get and remove cookies.
Add @leandromatos/nuxt-cookie
dependency to your project.
yarn add @leandromatos/nuxt-cookie
or
npm install @leandromatos/nuxt-cookie
Add @leandromatos/nuxt-cookie
to the modules
section of nuxt.config.js
.
{
modules: ['@leandromatos/nuxt-cookie']
}
After add the module on your project, you can access it through the $cookie
.
$cookie.set(name, value, options)
name
(string): Cookie name.value
(string orobject|array|boolean): Cookie value.options
(object): Same options as the cookie.serialize method of the cookie module.
Usage on server-side:
// Nuxt middleware
export default ({ app: { $cookie } }) => {
$cookie.set('cookie-name', 'server-cookie-value', {
path: '/',
})
})
Usage on client-side:
// Vue component
export default {
mounted() {
this.$cookie.set('cookie-name', 'client-cookie-value', {
path: '/',
})
}
}
get(name)
name
(string): Cookie name.
// Nuxt middleware
export default ({ app: { $cookie } }) => {
const cookie = $cookies.get('cookie-name')
}
// Vue component
export default {
mounted() {
const cookie = this.$cookie.get('cookie-name')
}
}
remove(name, options)
name
(string): Cookie name.options
(object): Same options as the cookie.serialize method of the cookie module
// Nuxt middleware
export default ({ app: { $cookie } }) => {
const cookie = $cookies.remove('cookie-name')
}
// Vue component
export default {
mounted() {
const cookie = this.$cookie.remove('cookie-name')
}
}
- Clone this repository
- Install dependencies using
yarn install
ornpm install
- Start development server using
yarn run dev
ornpm run dev
Copyright (c) Leandro Matos [email protected]