anish2690 / vue-cookie-next

A vue 3 plugin for handling browser cookies with typescript support. Load and save cookies within your Vue 3 application
MIT License
53 stars 6 forks source link

TypeScript failed to compile this.$cookie #3

Closed xyshell closed 3 years ago

xyshell commented 3 years ago

Hi guys,

I'm using Vue3 and vue-cookie-next's this.$cookie in mounted() as the README says. But typescript throws me Failed to compile error. But the console prints everything just fine.

I tried to set "strict": false in the tsconfig.json and then everything works fine. Anyone knows what's happening here?

TS2339: Property '$cookie' does not exist on type '{ name: string; setup(): { authors: { data: never[]; }; }; mounted(): void; }'.
    28 |   },
    29 |   mounted() {
  > 30 |     this.$cookie.setCookie("username", "user1");
       |          ^^^^^^^
    31 |     console.log(this.$cookie.getCookie("username"));
    32 |   },
    33 | };
Schotsl commented 3 years ago

I'm having the same issue :(

daydaychao commented 3 years ago

Try to use this, it works

import { VueCookieNext as $cookie } from 'vue-cookie-next'

$cookie.setCookie('frontend', 'lalala')

<script lang="ts">
import { defineComponent, reactive, onMounted } from 'vue'
import { VueCookieNext as $cookie } from 'vue-cookie-next'

export default defineComponent({
  name: 'develope',
  setup() {
    onMounted(() => {
      $cookie.setCookie('frontend', 'lalala')
    })
  }
})
</script>
anish2690 commented 3 years ago

this should be fixed in (https://github.com/anish2690/vue-cookie-next/pull/5)