how to use router in a pinia store in Nuxt 3?
Hi guys!
How can I use router in a pinia store?
6 Replies
Same way you would in your pages, components, composables, etc
don't think so
I'm migrating a Vue 3 SSR project to Nuxt 3 and I have a store where I access the router using
this.router
and it returns undefined when I console it
Currently, I have a functionality that is broken because of that
Tried const router = useRouter()
but also returns undefined when I console itWhy would you be keeping the router stored in Pinia?
Are you looking for the nuxt context?
https://pinia.vuejs.org/core-concepts/plugins.html#Nuxt-js
https://pinia.vuejs.org/core-concepts/plugins.html#Introduction
Reading through this, it seems like you'd be needing to set like
Pinia 🍍
Intuitive, type safe, light and flexible Store for Vue
I made a plugin : export default function ({ $pinia, $router }) {
$pinia.use(({ store }) => {
store.$router = $router
})
} and now its working
@Kealanau I just want to send the user to a page if it match some conditions
I was able to access it this way
there's also the option to use
navigateTo()
when only to redirect and there's no need to access the router object