vue-cookie-next
A simple Vue 3 plugin for handling browser cookies with typescript support
Installation
Browser
<html lang="en">
<head>
<script src="https://unpkg.com/vue/dist/vue.js"></script>
</head>
<body>
<div id="app"></div>
</body>
<script type="module">
import { VueCookieNext } from 'https://unpkg.com/vue-cookie-next@1.0.0/dist/vue-cookie-next.esm-bundler.js'
const CookieTest = {
mounted() {
this.$cookie.setCookie('username', 'user1')
console.log(this.$cookie.getCookie('username'))
},
}
Vue.createApp(CookieTest).use(VueCookieNext).mount('#app')
</script>
</html>
Package Managers
npm install vue-cookie-next
//or
yarn add vue-cookie-next
import { createApp } from 'vue'
import { VueCookieNext } from 'vue-cookie-next'
import App from 'App.vue'
const app = createApp(App)
app.use(VueCookieNext)
app.mount('#app')
VueCookieNext.config({ expire: '7d' })
VueCookieNext.setCookie('theme', 'default')
VueCookieNext.setCookie('hover-time', { expire: '1s' })
Composition API
import { defineComponent } from 'vue'
import { useCookie } from 'vue-cookie-next'
defineComponent({
setup() {
const cookie = useCookie()
cookie.setCookie('theme', 'dark')
cookie.removeCookie('hover-time')
},
});
API Options
syntax format: [this | VueCookieNext].$cookie.[method]
VueCookieNext.config({
expire: '1d',
path: '/',
domain: '',
secure: '',
sameSite: '',
})
this.$cookie.setCookie(keyName, value, {
expire: '1d',
path: '/',
domain: '',
secure: '',
sameSite: '',
})
this.$cookie.getCookie(keyName)
this.$cookie.removeCookie(keyName, {
path: '/',
domain: '',
})
this.$cookie.isCookieAvailable(keyName)
this.$cookie.keys()
Example Usage
set global config
import { VueCookieNext } from 'vue-cookie-next'
VueCookieNext.config({ expire: '30d' })
VueCookieNext.config({ expire: '7d', secure: true })
VueCookieNext.config({ expire: new Date(2019, 03, 13).toUTCString() })
VueCookieNext.config({ expire: 60 * 60 * 24 * 30 })
support json object
var user = {
user_id: 1,
name: 'Ben',
session: '75442486-0878-440c-9db1-a7006c25a39f',
session_start_time: new Date(),
}
this.$cookie.setCookie('user', user)
console.log(this.$cookie.getCookieCookie('user').name)
set expire times
Suppose the current time is : Sat, 11 Mar 2017 12:25:57 GMT
Following equivalence: 1 day after, expire
Support chaining sets together
this.$cookie
.setCookie('user_session', '75442486-0878-440c-9db1-a7006c25a39f')
.setCookie('user_session', '75442486-0878-440c-9db1-a7006c25a39f', {
expire: '1d',
})
.setCookie('user_session', '75442486-0878-440c-9db1-a7006c25a39f', {
expire: '1D',
})
.setCookie('user_session', '75442486-0878-440c-9db1-a7006c25a39f', {
expire: 60 * 60 * 24,
})
.setCookie('user_session', '75442486-0878-440c-9db1-a7006c25a39f', {
expire: new Date(2017, 03, 12),
})
.setCookie('user_session', '75442486-0878-440c-9db1-a7006c25a39f', {
expire: 'Sat, 13 Mar 2017 12:25:57 GMT',
})
set expire times, input number type
this.$cookie.setCookie('default_unit_second', 'input_value', { expire: 1 })
this.$cookie.setCookie('default_unit_second', 'input_value', {
expire: 60 + 30,
})
this.$cookie.setCookie('default_unit_second', 'input_value', {
expire: 60 * 60 * 12,
})
this.$cookie.setCookie('default_unit_second', 'input_value', {
expire: 60 * 60 * 24 * 30,
})
set expire times - end of browser session
this.$cookie.setCookie('default_unit_second', 'input_value', { expire: 0 })
set expire times , input string type
Unit | full name |
---|
y | year |
m | month |
d | day |
h | hour |
min | minute |
s | second |
Unit Names Ignore Case
not support the combination
not support the double value
this.$cookie.setCookie('token', 'GH1.1.1689020474.1484362313', {
expire: '60s',
})
this.$cookie.setCookie('token', 'GH1.1.1689020474.1484362313', {
expire: '30MIN',
})
this.$cookie.setCookie('token', 'GH1.1.1689020474.1484362313', {
expire: '24d',
})
this.$cookie.setCookie('token', 'GH1.1.1689020474.1484362313', {
expire: '4m',
})
this.$cookie.setCookie('token', 'GH1.1.1689020474.1484362313', {
expire: '16h',
})
this.$cookie.setCookie('token', 'GH1.1.1689020474.1484362313', {
expire: '3y',
})
this.$cookie.setCookie('token', 'GH1.1.1689020474.1484362313', {
expire: new Date(2017, 3, 13).toUTCString(),
})
this.$cookie.setCookie('token', 'GH1.1.1689020474.1484362313', {
expire: 'Sat, 13 Mar 2017 12:25:57 GMT ',
})
set expire support date
var date = new Date()
date.setDate(date.getDate() + 1)
this.$cookie.setCookie('token', 'GH1.1.1689020474.1484362313', {
expire: date,
})
set never expire
this.$cookie.setCookie('token', 'GH1.1.1689020474.1484362313', {
expire: Infinity,
})
this.$cookie.setCookie('token', 'GH1.1.1689020474.1484362313', { expire: -1 })
remove cookie
this.$cookie.setCookie('token', 'value')
this.$cookie.removeCookie('token')
this.$cookie.setCookie('token', value, { domain: 'domain.com' })
this.$cookie.removeCookie('token', { domain: 'domain.com' })
set other arguments
this.$cookie.setCookie('use_path_argument', 'value', {
expire: '1d',
path: '/app',
})
this.$cookie.setCookie('use_path_argument', 'value', { domain: 'domain.com' })
this.$cookie.setCookie('use_path_argument', 'value', {
secure: true,
})
this.$cookie.setCookie('use_path_argument', 'value', { sameSite: 'Lax' })
other operation
this.$cookie.isCookieAvailable("user_session")
this.$cookie.getCookie("user_session");
this.$cookie.removeCookie("user_session");
this.$cookie.keys().join("\n");
this.$cookie.keys().forEach(cookie => this.$cookie.removeCookie(cookie))
[this | VueCookieNext].$cookie.[method]
⚠️ Warning
$cookie key names Cannot be set to ['expires','max-age','path','domain','secure','SameSite']
🌸 Thanks
This project is heavily inspired by the following awesome projects.
Thanks!