Vue function that provides way to read, set and remove a cookie.
interface UseCookieOptions {
isParsing: boolean
serializer?: SerializerFunction
deserializer?: DeserializerFunction
}
function useCookie(
cookieName: string,
options?: UseCookieOptions,
runOnMount?: boolean
): {
cookie: Ref<any>
getCookie: () => void
setCookie: (newVal: any, options?: CookieSerializeOptions | undefined) => void
removeCookie: (options?: CookieSerializeOptions | undefined) => void
}
cookieName: string
the cookie name you wish to get/set/removeoptions: UseCookieOptions
isParsing: boolean
whether to enable parsing the cookie value or not,false
by defaultserializer: SerializerFunction
a custom serializer,JSON.stringify
by defaultdeserializer: DeserializerFunction
a custom deserializer,JSON.parse
by default
runOnMount: boolean
whether to get the cookie on mount or not,true
by default
cookie: Ref<any>
the cookie value, it can be null, a string or a JSON object/arraygetCookie: Function
get the cookie valuesetCookie: Function
set the cookie valuenewVal: any
: the value to set, can be a string or an object/arrayoptions?: CookieSerializeOptions
: a cookie options object
removeCookie: Function
delete the cookieoptions?: CookieSerializeOptions
: a cookie options object
<template>
<div>
<div>Cookie: {{ cookie }}</div>
<button @click="getCookie">Get cookie</button>
<button @click="setCookie('Value here')">Set cookie</button>
<button @click="removeCookie">Remove cookie</button>
</div>
</template>
<script lang="ts">
import Vue from 'vue'
import { useCookie } from 'vue-use-kit'
export default Vue.extend({
name: 'UseCookieDemo',
setup() {
const { cookie, getCookie, setCookie, removeCookie } = useCookie(
'i_love_cookies'
)
return {
cookie,
getCookie,
setCookie,
removeCookie
}
}
})
</script>