-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Pass globals compatible with Setup Stores #1784
Comments
This would be fantastic! Right now I'm defining the pinia.use(() => ({
myPlugin: { someFunction: () => console.log('Hello world') },
}))
...
function doStuff (this: Store) {
this.myPlugin.someFunction();
} |
It's pretty hard to use Pinia setup API without being able to pass in globals through plugins. Passing them through actions from components generates antipatterns logic. This is quite an important feature to see in Pinia. |
If you really want to use the Pinia setup API like I am, the only workaround I find is to wrap the store with another function and then pass the context from any component. Although I don't know any bad sides about this approach yet // foo.vue
const { submitForm, etc } = useBarStore(customContext)();
// stores/bar.js
export const useBarStore = (customContext) =>
defineStore("...", () => {
// ...
}); |
There are discussions to make it possible to make BTW doing this function useBarStore(context) {
return defineStore('...', => {
})()
} should be avoided as you are not supposed to define a store with the same id multiple times. |
Allows accessing globals provided at the app level with `app.provide()` via a regular `inject()` call as long as you have access to the application. Useful for Pinia, vue-router, Nuxt, Quasar, and other advanced use cases. - vuejs/pinia#1784
A workaround to have globals used within setup stores is to pick them up from the app (if possible). here is an example with the router: import { getActivePinia } from 'pinia'
export function useRouter() {
return getActivePinia()?._a.config.globalProperties.$router!
}
export function useRoute() {
return getActivePinia()?._a.config.globalProperties.$route!
} These functions should work properly within a setup stores as if you were callig the original router composables within a |
Allows accessing globals provided at the app level with `app.provide()` via a regular `inject()` call as long as you have access to the application. Useful for Pinia, vue-router, Nuxt, Quasar, and other advanced use cases. - vuejs/pinia#1784
Allows accessing globals provided at the app level with `app.provide()` via a regular `inject()` call as long as you have access to the application. Useful for Pinia, vue-router, Nuxt, Quasar, and other advanced use cases. - vuejs/pinia#1784
There is a very annoying lack of documentation about this for Vue Router, and in a lesser way for Pinia. This issue is extremely informative. I am guessing that there is some invisible context that Assuming this solution was implemented - is defineStore('store', ({ router }) => {
// ...
}) If not, thanks for the amazing workaround (which is reactive?).
If discussion has made it into writing would you please include a link to it? Edit: Upgraded Pinia to 3.3.4 and now |
Yes, you can just call useRouter() within a setup store now! There is a note about this in docs |
Thank you so much for this! I'll add our user story for some closure. We believe in the rationale behind the composition API and have fully migrated from the options API. We avoid Vuex stores since, although convenient for migration, they are based on a legacy tool and provide no benefit. We use setup stores as their composition API syntax reduces required knowledge. We want to use the URL as a backend for state. That is, to have URL query parameters be the single source of truth for the state they represent. This avoids having one state for the app (variables) and another for the URL (query params) and having to keep them in sync (for example by reading initial values from the URL and adding watchers to variables that update the URL). All route interaction can easily be abstracted into store computed variables, for easier conceptualization and better maintainability. Even if this was possible before (we struggled), it is a huge improvement to maintainability imo. |
Hello, in my project I have already used the latest version, but when using const route = useRoute(); I still get the prompt my code
vue: 3.3.4 |
It must be outside of logout. Think of the setup store as a component script setup: you wouldn’t write useRoute within a function either |
Thank you very much for your reply, I tried to use |
It's inside defineStore('...', () => {
const route = useRoute()
}) Like in a script setup component |
In Nuxt I get this error when doing this:
I'm just trying to do a getter like so: const route = useRoute();
function currentTopic() {
const slug = route.params.topic || route.matched[0]?.meta?.topic;
return topics.value.find((t) => t.slug === slug);
}
return {
currentTopic,
} |
My bad, I change my regular pinia store to a setup store to have access to const route = useRoute();
const currentTopic = computed(() => {
const slug = route.params.topic || route.matched[0]?.meta?.topic;
return topics.value.find((t) => t.slug === slug);
});
return {
currentTopic,
} |
I have a plugin that adds |
I'm really confused by @mrleblanc101 and the example here. In this code using pinia 2.1.7: import { useRoute } from 'vue-router'
export const useAppStore = defineStore('app', () => {
const route1 = useRoute()
const toolbarLabel = computed(() => {
const route2 = useRoute()
console.log('computed route1:', route1)
console.log('computed route2:', route2) I see:
in the console. I see so many examples around where |
What problem is this solving
Currently we can add globals to options stores with
But this only affect option stores
Proposed solution
Similar to
app.config.globalProperties
: https://vuejs.org/api/application.html#app-config-globalpropertiesThen set up stores could receive these properties as an argument:
Option stores could still use the properties through
this
.Describe alternatives you've considered
pinia
and usegetActivePinia()
. This also works with TypeScript, it not semantic thoughuseRouter()
and similar composables within setup stores but this requires a way for libraries to set the active Vue App, so this requires Vue to implement some kind ofsetCurrentApp(app)
(needs PR and discussion)The text was updated successfully, but these errors were encountered: