Skip to content

can I call useRouter() inside store? #442

Closed Answered by posva
husayt asked this question in Help and Questions
Discussion options

You must be logged in to vote

Like any function based on inject(), you won't be able to use it outside of setup(). With the plugin API you can add a router property to each store:

pinia.use(() => ({ router })

createApp(App).use(router).use(pinia).mount('#app')

And type it with

// extensions.d.ts
import 'pinia'
import { Router } from 'vue-router'

declare module 'pinia' {
  export interface PiniaCustomProperties {
    router: Router
  }
}

The plugin API is a WIP at #416
The ongoing docs are at https://deploy-preview-416--pinia.netlify.app/core-concepts/plugins.html#typescript

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by husayt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants