From 62965b81e416e9cb43256a5dbb5e3b99c2573248 Mon Sep 17 00:00:00 2001 From: Matthew Gamble Date: Sun, 25 Jul 2021 01:09:17 +1000 Subject: [PATCH] Add support for Vue's composition API (#6) * Add support for Vue's composition API * Fix: README api import typo Co-authored-by: Anish George --- README.md | 15 +++++++++++++++ src/VueCookieNext.ts | 1 + src/index.ts | 2 ++ src/useApi.ts | 6 ++++++ 4 files changed, 24 insertions(+) create mode 100644 src/useApi.ts diff --git a/README.md b/README.md index 4675009..2abdeba 100644 --- a/README.md +++ b/README.md @@ -52,6 +52,21 @@ VueCookieNext.setCookie('theme', 'default') VueCookieNext.setCookie('hover-time', { expire: '1s' }) ``` +### Composition API + +```ts +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]** diff --git a/src/VueCookieNext.ts b/src/VueCookieNext.ts index 4fd0326..a0a9fc2 100644 --- a/src/VueCookieNext.ts +++ b/src/VueCookieNext.ts @@ -50,6 +50,7 @@ const defaultConfig: CookieConfigOptions = { export const VueCookieNext: IVueCookieNext = { install: function (app: App) { app.config.globalProperties.$cookie = this + app.provide('cookie', this) }, config: function (options: CookieConfigOptions) { const { expire, path, domain, secure, sameSite } = options diff --git a/src/index.ts b/src/index.ts index 7bdd7b2..5c72498 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1 +1,3 @@ export { VueCookieNext } from './VueCookieNext' + +export * from './useApi' diff --git a/src/useApi.ts b/src/useApi.ts new file mode 100644 index 0000000..9e51bd7 --- /dev/null +++ b/src/useApi.ts @@ -0,0 +1,6 @@ +import { inject } from 'vue' +import type { IVueCookieNext } from './VueCookieNext' + +export function useCookie(): IVueCookieNext { + return inject('cookie')! +}