vuejs / composition-api

Composition API plugin for Vue 2
https://composition-api.vuejs.org/
MIT License
4.19k stars 343 forks source link

【Bug】 Uncaught Error: [vue-composition-api] must call Vue.use(VueCompositionAPI) before using any function #895

Closed stephane303 closed 2 years ago

stephane303 commented 2 years ago

I must reopen this issue, and I am referring to https://github.com/vuejs/composition-api/issues/621.

I have found many cases of examples where it's perfectly valid and necessary to put the ref outside setup or before vue is initialized: https://www.smashingmagazine.com/2021/06/managing-shared-state-vue3 https://markus.oberlehner.net/blog/vue-composition-api-composables/

Especially if the singleton pattern needs to be implemented.

I understand it could be a limitation of the composition-api for vue2, but how can the singleton pattern be solved ?

FYI: I am running a project that is using this (ref outside the function, and it's working fine).

I am starting having issues, when upgrading vue-demi to 0.12.1.

HIMISOCOOL commented 2 years ago

Perhaps you can initialize your global scope stuff in the setup method of the root component?

Alternatively I had a similar issue with a webapp using the monorepo pattern and the solution ended up using vue-cli lazy components loading via webpack to split off functionality via the main entrypoint.

stephane303 commented 2 years ago

Finally I used the Option 1 of https://stackoverflow.com/questions/69846717/vue2-and-composition-api-cannot-import-store-error-vue-composition-api-must, everywhere I had used ref outside a setup function, and so far everything is ok with the latest version.