6

GitHub - Gumball12/pinia-plugin-on-mounted: 🍍 Mounted hook in Pinia.

 1 year ago
source link: https://github.com/Gumball12/pinia-plugin-on-mounted
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

pineapple pinia-plugin-on-mounted

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.

rocket Quick Start

1. Install the plugin

npm i pinia-plugin-on-mounted
yarn add pinia-plugin-on-mounted # for yarn
pnpm add pinia-plugin-on-mounted # for pnpm

2. Add the plugin the Pinia

import { createPinia } from 'pinia';
import { PiniaPluginOnMounted } from 'pinia-plugin-on-mounted';

const pinia = createPinia();
pinia.use(PiniaPluginOnMounted);

3. Use the plugin

import { onPiniaMounted } from 'pinia-plugin-on-mounted';

onPiniaMounted(() => {
  // do something
});

book Use cases

1. Don't care if Pinia is initialized

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.

taco API

onPiniaMounted(callback: () => void): void

Register a callback to be called when the Pinia is mounted.

License

MIT


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK