If you need this hook, you're probably using Pinia incorrectly. See this comment!
Don't worry about whether Pinia is initialized, just use your store.
Safely using the Pinia store outside of components.
There are some cases where you need to invoke callbacks when Pinia is mounted. This module can be used in such situations.
npm i pinia-plugin-on-mounted
yarn add pinia-plugin-on-mounted # for yarn
pnpm add pinia-plugin-on-mounted # for pnpm
import { createPinia } from 'pinia';
import { PiniaPluginOnMounted } from 'pinia-plugin-on-mounted';
const pinia = createPinia();
pinia.use(PiniaPluginOnMounted);
import { onPiniaMounted } from 'pinia-plugin-on-mounted';
onPiniaMounted(() => {
// do something
});
import { onPiniaMounted } from 'pinia-plugin-on-mounted';
import { useUserStore } from './user';
// Pinia initialization errors may occur
// like "Uncaught Error: [🍍]: getActivePinia was called with no active Pinia. Did you forget to install pinia?"
const userStore = useUserStore();
onPiniaMounted(() => {
// No matter what you do
const userStore = useUserStore();
});
You can use your store outside of the store without worrying about whether or not Pinia is initialized.
Register a callback to be called when the Pinia is mounted.