Accessing route from getters using option store #2732
-
Hi guys, i've got a store needs to read a set of filter query parameters from the route and set it to state on the store. My store is currently set up in options store syntax but i've not been able to find a way to access the route object when using options store syntax. The closest I've seen is a solution on stack overflow that references to an old discussion but that uses setup stores syntax. Ideally i don't want to have to convert my entire pinia store that i've already written to setup store syntax so is there a way for me to access the route's query params using options store? Many thanks! ❤️ I'm looking for something like below: export const filterStore = defineStore('filtersStore', {
state: () => ({ ... }),
actions: {
mapUrlQueryParamsToFilters() {
const { firstName, lastName, role } = this.route.query // is something like this even possible?
...
}
}
}) |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Actually nevermind, I found out. Though, not sure if this is best way but it's working for me atm. If it can be improved on, please let me know! Will report back if there are any problems... // main.js
import { useRoute } from 'vue-router'
const pinia = createPinia();
pinia.use(({ store }) => {
store.route = useRoute();
})
app.use(pinia);
// inside your store...
const store = defineStore('myStore', {
state: () => ({ ... })
actions: {
mapUrlQueryParamsToFilters() {
console.log('route', this.route); // entire route object is here
}
}
}) |
Beta Was this translation helpful? Give feedback.
Actually nevermind, I found out.
Though, not sure if this is best way but it's working for me atm. If it can be improved on, please let me know!
Will report back if there are any problems...