vuejs / pinia

🍍 Intuitive, type safe, light and flexible Store for Vue using the composition api with DevTools support
https://pinia.vuejs.org
MIT License
13.11k stars 1.05k forks source link

Provide/inject in @pinia/nuxt broken #2745

Closed tdekoning closed 2 months ago

tdekoning commented 2 months ago

Reproduction

https://stackblitz.com/edit/github-ptqlwv-b7gmky?file=app.vue

Steps to reproduce the bug

Just load up the application and see the difference between the injected value from the store and the directly injected value in the component. The value injected into the store is empty, while the component has the correct value

Expected behavior

I expect provide/inject to work in pinia stores.

Actual behavior

The injected value in the store is empty. Also note the [Vue warn]: injection "myKey" not found. warning in the console.

Additional information

This is using the @pinia/nuxt package inside of a nuxt 3.12.4 application.

This injection pattern used to work with nuxt 3.11.2 and @pinia/nuxt 0.5.1 and pinia 2.1.7

bperel commented 2 months ago

Reproduced on vanilla Vue as well:

https://stackblitz.com/edit/vitejs-vite-l2tkb3?file=src%2FApp.vue

rhelling commented 2 months ago

+1 have the same issue

posva commented 2 months ago

This is the intended behavior, it was broken before. See https://github.com/vuejs/core/issues/11602

tdekoning commented 2 months ago

@posva thanks for the explanation, I wasn't aware of that.

bperel commented 2 months ago

@posva Is there a place in the docs where instructions about how to achieve provide/inject using the app instance can be performed? I tried the following but it didn't work either:

getCurrentInstance().appContext.app.provide('myGlobalKey', 'my-global-value');
posva commented 2 months ago

yes, that works. I wouldn't do it that way though, I would only use app.provide() directly after creating the app.