Can you make stores available in all components? #1197
-
New to Pinia, so apologies in advance. Using Nuxt. Is there a way that all components can automatically have access to all stores? At the moment I have some boilerplate code in a component which:
This code works, but would be the same in all components. Is there a way to make all of this happen automatically? |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 2 replies
-
what about provide a function which init all store there ? export const allStore = () => {
return {
demo: useDemoStore(),
counter: useCounterStore(),
};
}; |
Beta Was this translation helpful? Give feedback.
-
I'm also coming from vuex and wanted to look for a similiar solution to (I'm using Vue 3 in this example.) In the main.js I created a global variable called
that way i can access the variable through Then I initialized all the stores in App.vue
Done. :D After that you should be able to access the stores like in vuex via |
Beta Was this translation helpful? Give feedback.
-
Setting One solution that works with both is using auto imports. By setting the |
Beta Was this translation helpful? Give feedback.
-
This method works except reactivity. Do you have any suggestions to add Reactivity to pinia Store using this method. I'm trying to change the Vuex store to pinia but there is a lot of this.$stores usage in the project. |
Beta Was this translation helpful? Give feedback.
Setting
this.$store
or similar is fine for prototyping but cancels out some of the improvements pinia brings like automatic dynamic modules and especially typing and autocompletion.One solution that works with both is using auto imports. By setting the
dirs
option to the folder where your stores are defined, you should be able to directly calluseMyStore()
everywhere needed and still benefit from TypeScript and better code splitting (dynamic modules)