Using a store outside of a component
Pinia stores rely on the
pinia
instance to share the same store instance across all calls. Most of the time, this works out of the box by just calling your
useStore()
function. For example, in
setup()
, you don't need to do anything else. But things are a bit different outside of a component. Behind the scenes,
useStore()
injects
the
pinia
instance you gave to your
app
. This means that if the
pinia
instance cannot be automatically injected, you have to manually provide it to the
useStore()
function. You can solve this differently depending on the kind of application you are writing.
Single Page Applications
If you are not doing any SSR (Server Side Rendering), any call of
useStore()
after installing the pinia plugin with
app.use(pinia)
will work:
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.
Let's take a look at this example of using a store inside of a navigation guard with Vue Router: