setActivePinia,
piniaSymbol,
} from './rootStore'
-import { ref, App, markRaw, effectScope } from 'vue-demi'
+import { ref, App, markRaw, effectScope, isVue2 } from 'vue-demi'
import { registerPiniaDevtools, devtoolsPlugin } from './devtools'
import { IS_CLIENT } from './env'
import { StateTree, StoreGeneric } from './types'
const pinia: Pinia = markRaw({
install(app: App) {
- pinia._a = app
- app.provide(piniaSymbol, pinia)
- app.config.globalProperties.$pinia = pinia
- if (IS_CLIENT) {
- // this allows calling useStore() outside of a component setup after
- // installing pinia's plugin
- setActivePinia(pinia)
- if (__DEV__) {
- registerPiniaDevtools(app, pinia)
+ if (!isVue2) {
+ pinia._a = app
+ app.provide(piniaSymbol, pinia)
+ app.config.globalProperties.$pinia = pinia
+ if (IS_CLIENT) {
+ // this allows calling useStore() outside of a component setup after
+ // installing pinia's plugin
+ setActivePinia(pinia)
+ if (__DEV__) {
+ registerPiniaDevtools(app, pinia)
+ }
}
+ toBeInstalled.forEach((plugin) => _p.push(plugin))
}
- toBeInstalled.forEach((plugin) => _p.push(plugin))
},
use(plugin) {
- if (!this._a) {
+ if (!this._a && !isVue2) {
toBeInstalled.push(plugin)
} else {
_p.push(plugin)
function setup() {
if (!initialState && (!__DEV__ || !hot)) {
- if (isVue2) {
+ // only use set in Vue 2 if it's not for HMR
+ if (isVue2 && (!__DEV__ || !id.startsWith('__hot'))) {
set(pinia.state.value, id, state ? state() : {})
} else {
pinia.state.value[id] = state ? state() : {}
const initialState = pinia.state.value[$id] as UnwrapRef<S> | undefined
if (!initialState && __DEV__ && !hot) {
- // should be set in Vue 2
- pinia.state.value[$id] = {}
+ // only use set in Vue 2 if it's not for HMR
+ if (isVue2 && (!__DEV__ || !$id.startsWith('__hot'))) {
+ set(pinia.state.value, $id, {})
+ } else {
+ pinia.state.value[$id] = {}
+ }
}
const hotState = ref({} as S)
hotState.value[key] = toRef(setupStore as any, key)
// createOptionStore already did this
} else if (!buildState) {
- pinia.state.value[$id][key] = prop
+ if (isVue2) {
+ set(pinia.state.value[$id], key, prop)
+ } else {
+ pinia.state.value[$id][key] = prop
+ }
// TODO: avoid if state exists for SSR
}
import type { Plugin } from 'vue-demi'
-import { piniaSymbol } from './rootStore'
+import { registerPiniaDevtools } from './devtools'
+import { IS_CLIENT } from './env'
+import { Pinia, piniaSymbol, setActivePinia } from './rootStore'
/**
* Vue 2 Plugin that must be installed for pinia to work. Note **you don't need
beforeCreate() {
const options = this.$options
if (options.pinia) {
+ const pinia = options.pinia as Pinia
// HACK: taken from provide(): https://github.com/vuejs/composition-api/blob/master/src/apis/inject.ts#L30
/* istanbul ignore else */
if (!(this as any)._provided) {
set: (v) => Object.assign(provideCache, v),
})
}
- ;(this as any)._provided[piniaSymbol as any] = options.pinia
+ ;(this as any)._provided[piniaSymbol as any] = pinia
// propagate the pinia instance in an SSR friendly way
// avoid adding it to nuxt twice
/* istanbul ignore else */
if (!this.$pinia) {
- this.$pinia = options.pinia
+ this.$pinia = pinia
+ }
+
+ pinia._a = this as any
+ if (IS_CLIENT) {
+ // this allows calling useStore() outside of a component setup after
+ // installing pinia's plugin
+ setActivePinia(pinia)
+ if (__DEV__) {
+ registerPiniaDevtools(pinia._a, pinia)
+ }
}
} else if (!this.$pinia && options.parent && options.parent.$pinia) {
this.$pinia = options.parent.$pinia