Skip to content

I can't use pinia outside of component setup #664

Discussion options

You must be logged in to vote

That error is happening because you are trying to create a store before calling app.use(pinia) (or however you are initializing it)

Have a look at the docs here: https://pinia.esm.dev/core-concepts/outside-component-usage.html
Most importantly:

The easiest way to ensure this is always applied is to defer calls of useStore() by placing them inside functions that will always run after pinia is installed.

I can't see in your screenshots above where exactly you planned on using the userStore, but if it's in an Axios interceptor then call it within the interceptor function:

axios.interceptors.request.use(function (config) {
  const userStore = useUserStore()
  config.headers.Authorization = u…

Replies: 2 comments 5 replies

Comment options

You must be logged in to vote
4 replies
@BenShelton
Comment options

@rangeldor
Comment options

@rangeldor
Comment options

@BenShelton
Comment options

Answer selected by posva
Comment options

You must be logged in to vote
1 reply
@rangeldor
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
3 participants