+++ /dev/null
-import type { Plugin } from 'vue'
-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
- * this plugin if you are using Nuxt.js**. Use the `buildModule` instead:
- * https://pinia.vuejs.org/ssr/nuxt.html.
- *
- * @example
- * ```js
- * import Vue from 'vue'
- * import { PiniaVuePlugin, createPinia } from 'pinia'
- *
- * Vue.use(PiniaVuePlugin)
- * const pinia = createPinia()
- *
- * new Vue({
- * el: '#app',
- * // ...
- * pinia,
- * })
- * ```
- *
- * @param _Vue - `Vue` imported from 'vue'.
- */
-export const PiniaVuePlugin: Plugin = function (_Vue) {
- // Equivalent of
- // app.config.globalProperties.$pinia = pinia
- _Vue.mixin({
- 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/main/src/apis/inject.ts#L31
- /* 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] = pinia
-
- // propagate the pinia instance in an SSR friendly way
- // avoid adding it to nuxt twice
- /* istanbul ignore else */
- if (!this.$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 (__USE_DEVTOOLS__ && IS_CLIENT) {
- registerPiniaDevtools(pinia._a, pinia)
- }
- } else if (!this.$pinia && options.parent && options.parent.$pinia) {
- this.$pinia = options.parent.$pinia
- }
- },
- destroyed() {
- delete this._pStores
- },
- })
-}