Skip to content

Getting handle on router added globally in a store with a "setup function" #1717

Discussion options

You must be logged in to vote

One could attach the router instance to the pinia instance and retrieve through the active pinia:

import { getActivePinia } from 'pinia'

const pinia = createPinia()
pinia.router = router // extend the Pinia Interface for typings

const useRouter = () => getActivePinia().router as Router

You can use this useRouter() version within setup stores.

In Nuxt, since you don't manually create the router or the pinia, you can add a module or plugin to set the router to the pinia instance


Note that if you are not doing SSR, you can just directly import the router variable from your router file.

Replies: 1 comment 6 replies

Comment options

You must be logged in to vote
6 replies
@posva
Comment options

@demyte
Comment options

@posva
Comment options

@demyte
Comment options

@posva
Comment options

Answer selected by posva
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants