Skip to content

Commit

Permalink
Add support for Vue's composition API (#6)
Browse files Browse the repository at this point in the history
* Add support for Vue's composition API

* Fix: README api import typo

Co-authored-by: Anish George <anishgeorge2690@gmail.com>
  • Loading branch information
djmattyg007 and anish2690 authored Jul 24, 2021
1 parent be0a48b commit 62965b8
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 0 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]**
Expand Down
1 change: 1 addition & 0 deletions src/VueCookieNext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export { VueCookieNext } from './VueCookieNext'

export * from './useApi'
6 changes: 6 additions & 0 deletions src/useApi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { inject } from 'vue'
import type { IVueCookieNext } from './VueCookieNext'

export function useCookie(): IVueCookieNext {
return inject<IVueCookieNext>('cookie')!
}

0 comments on commit 62965b8

Please sign in to comment.