Getting handle on router added globally in a store with a "setup function" #1717
-
|
Hi all, I have setup my router in the store globally as showed in the docs: import type { Router } from 'vue-router'
export interface PiniaCustomProperties {
router: Router
}
}
const pinia = createPinia()
pinia.use(({ store }) => {
store.router = markRaw(router)
})And my store is being setup with a setup function: export const useGlobalStore = defineStore('global', () => {
// This leaves router as undefined
const router = useRouter()
})My google-fu is failing me trying to work out how to get a handle on that router instance that was injected as I need to use it because Thanks :) |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 6 replies
-
|
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 RouterYou can use this 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 |
Beta Was this translation helpful? Give feedback.





One could attach the router instance to the pinia instance and retrieve through the active pinia:
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
routervariable from your router file.