]> git.ipfire.org Git - thirdparty/vuejs/pinia.git/commitdiff
docs(nuxt): pinia plugins usage (#1581) [skip ci]
authorcx33 <lne128gene980@163.com>
Mon, 10 Oct 2022 20:51:04 +0000 (04:51 +0800)
committerGitHub <noreply@github.com>
Mon, 10 Oct 2022 20:51:04 +0000 (22:51 +0200)
packages/docs/core-concepts/plugins.md

index bd63afd9f3b8d06d8709d9888aac7da5b51c71e1..9513eb1650e0f120fcbc05eba5ed06d0dfd0b7fc 100644 (file)
@@ -365,6 +365,29 @@ There is also a `StoreGetters` type to extract the _getters_ from a Store type.
 
 When [using pinia alongside Nuxt](../ssr/nuxt.md), you will have to create a [Nuxt plugin](https://nuxtjs.org/docs/2.x/directory-structure/plugins) first. This will give you access to the `pinia` instance:
 
+```ts
+// plugins/myPiniaPlugin.js
+import { PiniaPluginContext } from 'pinia'
+
+function MyPiniaPlugin({ store }: PiniaPluginContext) {
+  store.$subscribe((mutation) => {
+    // react to store changes
+    console.log(`[🍍 ${mutation.storeId}]: ${mutation.type}.`)
+  })
+
+  // Note this has to be typed if you are using TS
+  return { creationTime: new Date() }
+}
+
+export default defineNuxtPlugin(({ $pinia }) => {
+  $pinia.use(MyPiniaPlugin)
+})
+```
+
+Note the above example is using TypeScript, you have to remove the type annotations `PiniaPluginContext` and `Plugin` as well as their imports if you are using a `.js` file.
+
+### Nuxt.js 2
+
 ```ts
 // plugins/myPiniaPlugin.js
 import { PiniaPluginContext } from 'pinia'
@@ -386,5 +409,3 @@ const myPlugin: Plugin = ({ $pinia }) => {
 
 export default myPlugin
 ```
-
-Note the above example is using TypeScript, you have to remove the type annotations `PiniaPluginContext` and `Plugin` as well as their imports if you are using a `.js` file.