|
| 1 | +# Cookie |
| 2 | + |
| 3 | +> The [cookie](https://developer.mozilla.org/en-US/docs/Web/API/Document/cookie). |
| 4 | +
|
| 5 | +Provides a wrapper to use [js-cookie](https://github.com/js-cookie/js-cookie). |
| 6 | + |
| 7 | +## Parameters |
| 8 | + |
| 9 | +```js |
| 10 | +import { useCookie } from "@vue-composable/cookie"; |
| 11 | + |
| 12 | +useCookie(key, defaultValue, defaultOptions); |
| 13 | +``` |
| 14 | + |
| 15 | +| Parameters | Type | Required | Description | |
| 16 | +| -------------- | -------- | -------- | -------------------------------------------------------------------------------------------------------------------------------------------- | |
| 17 | +| key | `String` | `true` | Cookie name | |
| 18 | +| defaultValue | `String` | `false` | Set default value when cookie is undefined | |
| 19 | +| defaultOptions | `Object` | `false` | Set default options when cookie is undefined. <br/> <br/> The [js-cookie options](https://github.com/js-cookie/js-cookie#cookie-attributes). | |
| 20 | + |
| 21 | +## State |
| 22 | + |
| 23 | +The `useCookie` function exposes the following reactive state: |
| 24 | + |
| 25 | +```js |
| 26 | +import { useCookie } from "@vue-composable/cookie"; |
| 27 | + |
| 28 | +const { cookie } = useCookie("some-cookie"); |
| 29 | +``` |
| 30 | + |
| 31 | +| State | Type | Description | |
| 32 | +| ------ | ----------- | ----------- | ----------- | ---------------- | |
| 33 | +| cookie | `Ref<String | null | undefined>` | The cookie value | |
| 34 | + |
| 35 | +## Methods |
| 36 | + |
| 37 | +The `useCookie` function exposes the following methods: |
| 38 | + |
| 39 | +```js |
| 40 | +import { useCookie } from "@vue-composable/cookie"; |
| 41 | + |
| 42 | +const { setCookie, removeCookie } = useCookie("some-cookie"); |
| 43 | +``` |
| 44 | + |
| 45 | +| Signature | Description | |
| 46 | +| -------------- | ----------------------- | |
| 47 | +| `setCookie` | Update the cookie value | |
| 48 | +| `removeCookie` | Remove the cookie | |
| 49 | + |
| 50 | +## Example |
| 51 | + |
| 52 | +<cookie-example /> |
| 53 | + |
| 54 | +### Code |
| 55 | + |
| 56 | +```vue |
| 57 | +<template> |
| 58 | + <div> |
| 59 | + cookie value: {{ cookie }} |
| 60 | + <p> |
| 61 | + <button @click="updateCookie">Update Cookie</button> |
| 62 | + </p> |
| 63 | + <p> |
| 64 | + <button @click="deleteCookie">Remove Cookie</button> |
| 65 | + </p> |
| 66 | + </div> |
| 67 | +</template> |
| 68 | +
|
| 69 | +<script> |
| 70 | +import { defineComponent } from "@vue/composition-api"; |
| 71 | +import { useCookie } from "@vue-composable/cookie"; |
| 72 | +
|
| 73 | +export default defineComponent({ |
| 74 | + name: "cookie-example", |
| 75 | +
|
| 76 | + setup() { |
| 77 | + let idx = 0; |
| 78 | +
|
| 79 | + let { cookie, setCookie, removeCookie } = useCookie("my-cookie"); |
| 80 | +
|
| 81 | + function updateCookie() { |
| 82 | + cookie.value = `my-cookie-${++idx}`; |
| 83 | + } |
| 84 | +
|
| 85 | + function deleteCookie() { |
| 86 | + removeCookie(); |
| 87 | + } |
| 88 | +
|
| 89 | + return { |
| 90 | + cookie, |
| 91 | +
|
| 92 | + updateCookie, |
| 93 | + deleteCookie, |
| 94 | + }; |
| 95 | + }, |
| 96 | +}); |
| 97 | +</script> |
| 98 | +
|
| 99 | +<style></style> |
| 100 | +``` |
0 commit comments