Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vue-cookies isn't working in composition api ? #93

Open
hakimov-dev opened this issue May 25, 2023 · 2 comments
Open

vue-cookies isn't working in composition api ? #93

hakimov-dev opened this issue May 25, 2023 · 2 comments

Comments

@hakimov-dev
Copy link

I've read how to use vue-cookies in composition api. int that place:
Screenshot_1

Then i use inject for using $cookie in composition api, but i received this bug:
image

What should i do ? How i can fix that ?

@julisch94
Copy link

Just to confirm, have you imported inject?

import { inject } from 'vue'

@Kharente-Deuh
Copy link

I'm currently working on vue3 with composition Api and Vite (cf vitesse https://github.com/antfu/vitesse/tree/main), and its working well for me.
Here's my implementation:

src/main.ts:

import { ViteSSG } from 'vite-ssg'
import { setupLayouts } from 'virtual:generated-layouts'
import * as cookies from 'vue-cookies'

// import Previewer from 'virtual:vue-component-preview'
import App from './App.vue'
import generatedRoutes from '~pages'
import './styles/main.scss'

const routes = setupLayouts(generatedRoutes)

// https://github.com/antfu/vite-ssg
export const createApp = ViteSSG(
  App,
  { routes, base: import.meta.env.BASE_URL },
  (ctx) => {
    ctx.app.use(cookies)
  },
)

src/composables/cookies.ts:

import { inject } from 'vue'
import type { VueCookies } from 'vue-cookies'

export function useCookies(): VueCookies {
  const $cookies = inject<VueCookies>('$cookies')
 // For js, use const $cookies = inject('$cookies')

  return $cookies as VueCookies
}

And then, use it like that:
src/App.vue:

<script setup lang="ts">
const cookies = useCookies()

cookies.set('Set-Cookie', 'HttpOnly;Secure;SameSite=Strict')
</script>

<template>
  <RouterView />
</template>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants