Skip to content

Register pinia plugin with @pinia/nuxt module? #622

Closed Answered by ttkoma
ttkoma asked this question in Help and Questions
Discussion options

You must be logged in to vote

Thanks for this module. But I didn't find an example in the documentation on how to use the Pinia plugins to extend the store using nuxt.
It turned out to be very simple, we can use nuxt plugins to access the pinia instance.
like this:

nuxt.config.js

plugins: [
    "./plugins/secretPiniaPlugin.js"
 ],

secretPiniaPlugin.js

function SecretPiniaPlugin(ctx) {
  ctx.store.$subscribe((mutation) => {
    // react to store changes
    console.log(`[STORE] ${mutation.storeId} ${mutation.type} changed`)
  })

  return { secret: 'the cake is a lie' }
}

export default function (nuxtCtx) {
  const { pinia } = nuxtCtx;
  pinia.use((piniaCtx) => SecretPiniaPlugin(piniaCtx));
}

Replies: 2 comments 2 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
2 replies
@posva
Comment options

@posva
Comment options

Answer selected by ttkoma
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants