-
-
Notifications
You must be signed in to change notification settings - Fork 8.4k
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
Values provided from app are undefined
when using inject
in Pinia setup store
#11602
Comments
You are correct that this issue is indeed related to #11502, but I believe the reproduction you provided demonstrates the expected behavior outlined in the Vue and Pinia documentation. In the Pinia documentation, it states:
Before #11502 was merged, the component context had a higher priority than the App context when using inject(). As a result, it was possible to inject component-provided properties in Pinia, which was incorrect. Since pinia is a global state manager, it seems not so meaningful to inject a state provided by a component instead of a globally provided state to a pinia store. The expected behavior is that Pinia's inject() should retrieve provides from the App context. After #11502 was merged, this behavior is now correct, so I believe the erroneous reproduction you provided actually reflects the expected behavior. If you want to use inject() in Pinia, you should consider using app.provide instead of using provide within the setup function of the App.vue component. |
Hmm, okay. This hasn't been documented anywhere as far as I know. Nuxt is very "app.vue-oriented", so I don't think I'm wrong to assume this would have worked. So what is the correct scope to provide into? It's really hard for people to understand all the scopes that come into play in a Vue app. Do I use |
I agree with @Gianthard-cyh. useNuxtApp().vueApp in app.vue <script setup lang="ts">
const app = useNuxtApp().vueApp;
app.provide('test', 'hi as primitive');
app.provide('testRef', ref('hi as ref'));
</script> see https://stackblitz.com/edit/github-tg21by-wjhhbr?file=app.vue |
Fantastic, thanks for the quick assistance! Would it be a good idea to make a note of this in the changelog? While I understand you've actually fixed a problem with #11502, it can be perceived as a breaking change as well. |
Vue version
3.4.37
Link to minimal reproduction
https://stackblitz.com/edit/github-tg21by-ju6avc?file=components%2FHi.vue
Steps to reproduce
Injections in Pinia stores are broken as of Vue 3.4.37. I'm assuming this to be a regression caused by this PR: #11502, as it seems to be the only change in 3.4.37 related to the provide/inject system.
Note that the reproduction is made with the Nuxt starter template, as I thought this was a Nuxt bug at first. Running Nuxt without SSR results in the same bug, so I'm expecting this to be a bug solely related to Vue, not
nuxt
,pinia
and/or@pinia/nuxt
.Repro for 3.4.37 (injections always
undefined
): see repro link aboveRepro for 3.4.36 (injections working as expected): https://stackblitz.com/edit/github-tg21by-mkqgva?file=components%2FHi.vue
What is expected?
Values provided by
app.vue
are available in the Pinia store by usinginject
.What is actually happening?
All injected values are
undefined
.System Info
Note that the bug is also present in the Stackblitz environment, so I don't think this is a system-specific problem.
Any additional comments?
No response
The text was updated successfully, but these errors were encountered: