DEV Community

Discussion on: Using Modules and Pinia to structure Nuxt 3 app

Collapse
 
andyjamesn profile image
andyjamesn

Could you explain a little about how the Pinia integration works?

In the module.ts you state

// Pinia store modules are auto imported

How are they setup to auto import? Is it because useBlogStore is being imported in the blob/[id] component?

Is it possible to use Pinia without a component in the module to store data eg: logged in user data and access it from components in the main app that is importing the module?

Collapse
 
jacobandrewsky profile image
Jakub Andrzejewski

So basically Pinia works in all places no matter where you import it in your application. If you import it in the module, this value will be then accessible from somewhere else. It is done because Pinia is a store that can be accessible from anywhere in the app.

Yes, it is possible to use Pinia without a component. It can be used in the composables as well or page. I just showed it as an example of usage :)

Collapse
 
andyjamesn profile image
andyjamesn

Excellent thank you! I did dive deep into their docs and came to this conclusion. I essentially import the stores in my plugin.ts file inside defineNuxtPlugin with const auth = useAuthStore(nuxtApp.$pinia)

This adds the store globally and then it is accessible anywhere in the app.