Skip to content

Commit

Permalink
feat(nuxt): nuxt plugin working with Vue 2
Browse files Browse the repository at this point in the history
  • Loading branch information
posva committed Aug 19, 2021
1 parent b6f0291 commit 2b3aa5f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
8 changes: 5 additions & 3 deletions packages/nuxt/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ const module = defineNuxtModule<PiniaNuxtOptions>({
defaults: {
disableVuex: true,
},
hooks: {},
setup(options, nuxt) {
// Disable default Vuex store (options.features only exists in Nuxt v2.10+)
if (nuxt.options.features && options.disableVuex) {
Expand Down Expand Up @@ -47,8 +46,11 @@ export default module

declare module '@nuxt/types' {
export interface Context {
// TODO: test out if correct
// pinia: Pinia
/**
* Pinia instance attached to the app.
*/
pinia: Pinia

/**
* Pinia instance attached to the app.
*/
Expand Down
8 changes: 5 additions & 3 deletions packages/nuxt/templates/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { isVue2, Vue2 } from 'vue-demi'
import { createPinia, setActivePinia, PiniaPlugin } from 'pinia'

if (isVue2) {
Vue2(PiniaPlugin)
Vue2.use(PiniaPlugin)
}

/**
Expand All @@ -13,13 +13,15 @@ const PiniaNuxtPlugin = (context, inject) => {
const pinia = createPinia()
// add $pinia to the context
inject('pinia', pinia)
// TO
// to allow accessing pinia without the $
// context.pinia = pinia
context.pinia = pinia

if (isVue2) {
// simulate new Vue({ pinia })
context.app.pinia = pinia
} else {
// TODO: does this work?
context.app.use(pinia)
}

setActivePinia(pinia)
Expand Down

0 comments on commit 2b3aa5f

Please sign in to comment.