"name": "Eduardo San Martin Morote",
"email": "posva13@gmail.com"
},
+ "funding": "https://github.com/sponsors/posva",
"scripts": {
"docs": "vitepress dev docs",
"docs:build": "vitepress build docs",
"vite": "^2.4.1",
"vitepress": "^0.15.6",
"vue": "^3.2.0-beta.1",
+ "vue2": "npm:vue@2",
"vue-promised": "^2.1.0",
"vue-router": "^4.0.10",
"yorkie": "^2.0.0"
export type { TestingOptions, TestingPinia } from './testing'
export { acceptHMRUpdate } from './hmr'
+
+export { PiniaPlugin } from './vue2-plugin'
-import { ComponentPublicInstance } from 'vue-demi'
-import {
+import type { ComponentPublicInstance } from 'vue-demi'
+import type {
GettersTree,
_Method,
StateTree,
}
return stores.reduce((reduced, useStore) => {
- // @ts-ignore: $id is added by defineStore
+ // @ts-expect-error: $id is added by defineStore
reduced[useStore.$id + mapStoreSuffix] = function (
this: ComponentPublicInstance
) {
toRefs,
Ref,
ref,
+ set,
+ isVue2,
} from 'vue-demi'
import {
StateTree,
function setup() {
if (!initialState && (!__DEV__ || !hot)) {
- pinia.state.value[id] = state ? state() : {}
+ if (isVue2) {
+ set(pinia.state.value, id, state ? state() : {})
+ } else {
+ pinia.state.value[id] = state ? state() : {}
+ }
}
// avoid creating a state in pinia.state.value
// watcher options for $subscribe
const $subscribeOptions: WatchOptions = { deep: true, flush: 'sync' }
/* istanbul ignore else */
- if (__DEV__) {
+ if (__DEV__ && !isVue2) {
$subscribeOptions.onTrigger = (event) => {
if (isListening) {
debuggerEvents = event
--- /dev/null
+import type { Plugin } from 'vue-demi'
+import { piniaSymbol } from './rootStore'
+
+/**
+ * Vue 2 Plugin that must be installed for pinia to work. Note **you don't need
+ * this plugin if you are using Nuxt.js**. Use the `buildModule` instead:
+ * https://pinia.esm.dev/ssr/nuxt.html.
+ *
+ * @example
+ * ```js
+ * import Vue from 'vue'
+ * import { PiniaPlugin, createPinia } from 'pinia'
+ *
+ * Vue.use(PiniaPlugin)
+ * const pinia = createPinia()
+ *
+ * new Vue({
+ * el: '#app',
+ * // ...
+ * pinia,
+ * })
+ * ```
+ *
+ * @param _Vue
+ */
+export const PiniaPlugin: Plugin = function (_Vue) {
+ // Equivalent of
+ // app.config.globalProperties.$pinia = pinia
+ _Vue.mixin({
+ beforeCreate() {
+ const options = this.$options
+ if (options.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) {
+ const provideCache = {}
+ Object.defineProperty(this, '_provided', {
+ get: () => provideCache,
+ set: (v) => Object.assign(provideCache, v),
+ })
+ }
+ ;(this as any)._provided[piniaSymbol as any] = options.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
+ }
+ } else if (!this.$pinia && options.parent && options.parent.$pinia) {
+ this.$pinia = options.parent.$pinia
+ }
+ },
+ destroyed() {
+ delete this._pStores
+ },
+ })
+}